Skip to content

Fix mainInParentDirectory failing when initdb is not on PATH#2398

Open
amitaibu wants to merge 10 commits intomasterfrom
fix-2397-mainInParentDirectory-initdb
Open

Fix mainInParentDirectory failing when initdb is not on PATH#2398
amitaibu wants to merge 10 commits intomasterfrom
fix-2397-mainInParentDirectory-initdb

Conversation

@amitaibu
Copy link
Copy Markdown
Collaborator

@amitaibu amitaibu commented Feb 12, 2026

Summary

  • route Postgres setup commands in IHP.IDE.Postgres through a direnv-aware command runner when wrapWithDirenv is enabled
  • update init-time calls (initdb, postgres, createdb, psql) and stale-lock cleanup (pg_ctl) to use the same execution strategy
  • switch SQL imports to psql -f invocation to avoid shell redirection and keep command execution consistent
  • fix Paths_ihp_ide data file resolution in ghci/dev mode so IHPSchema.sql is found when running mainInParentDirectory from a parent directory

Tests

  • added Test.IDE.PostgresSpec regression test for wrapped mode
  • extended the regression test to assert IHPSchema.sql is loaded from the resolved IHP_LIB path
  • verified with: echo -e ':l ihp-ide/Test/Main.hs\nmain' | ghci

Closes #2397

@amitaibu amitaibu marked this pull request as draft February 12, 2026 12:10
@amitaibu
Copy link
Copy Markdown
Collaborator Author

This is now working for me

image

@amitaibu amitaibu marked this pull request as ready for review February 12, 2026 12:18
@amitaibu
Copy link
Copy Markdown
Collaborator Author

@mpscholten this is AI generated, and I don't know enough to confirm it - so needs a proper review 😄

@amitaibu
Copy link
Copy Markdown
Collaborator Author

@codex GH action failed with

ihp-ide> 
ihp-ide>   ./Test/IDE/PostgresSpec.hs:52:26:
ihp-ide>   1) IHP.IDE.Postgres runs postgres setup through direnv in wrapped mode
ihp-ide>        predicate failed on: ["exec . initdb build/db/state --no-locale --encoding UTF8","exec . postgres -D build/db/state -k /nix/var/nix/builds/nix-51878-3021466917/source/build/test-postgres-direnv/build/db -c listen_addresses=","exec . createdb app -h /nix/var/nix/builds/nix-51878-3021466917/source/build/test-postgres-direnv/build/db","exec . psql -h /nix/var/nix/builds/nix-51878-3021466917/source/build/test-postgres-direnv/build/db -d app -f /nix/var/nix/builds/nix-51878-3021466917/source/data/IHPSchema.sql","exec . psql -h /nix/var/nix/builds/nix-51878-3021466917/source/build/test-postgres-direnv/build/db -d app -f Application/Schema.sql","exec . psql -h /nix/var/nix/builds/nix-51878-3021466917/source/build/test-postgres-direnv/build/db -d app -f Application/Fixtures.sql","exec . postgres -D build/db/state -k /nix/var/nix/builds/nix-51878-3021466917/source/build/test-postgres-direnv/build/db -c listen_addresses="]

@chatgpt-codex-connector
Copy link
Copy Markdown

To use Codex here, create an environment for this repo.

@amitaibu
Copy link
Copy Markdown
Collaborator Author

@copilot see #2398 (comment)

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 12, 2026

@amitaibu I've opened a new pull request, #2400, to work on those changes. Once the pull request is ready, I'll request review from you.

* Initial plan

* Fix test assertion to check for IHPSchema.sql in any path

The test was expecting IHPSchema.sql to be at a specific path in the test directory, but in the Nix build environment, getDataFileName resolves to the actual source directory. Changed the assertion to just check that IHPSchema.sql is loaded with -f flag, regardless of the absolute path.

Co-authored-by: amitaibu <125707+amitaibu@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: amitaibu <125707+amitaibu@users.noreply.github.com>
@amitaibu
Copy link
Copy Markdown
Collaborator Author

I had to stop the tests, as they seem to be stuck

image

Use #!/bin/sh instead of #!/usr/bin/env bash in fake test scripts since
/usr/bin/env is not available in the nix build sandbox. Also use exec in
the fake direnv wrapper for proper signal propagation, and add
create_group to initDatabase for consistent process cleanup.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@amitaibu amitaibu requested a review from mpscholten February 12, 2026 17:35
Comment thread dev/Paths_ihp_ide.hs Outdated
Comment on lines +35 to +39
[ "ihp-ide" </> path
, "ihp-ide" </> "data" </> path
, "IHP" </> "ihp-ide" </> path
, "IHP" </> "ihp-ide" </> "data" </> path
]
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can trim this list?

amitaibu and others added 2 commits February 15, 2026 17:16
IHP_LIB is always set by the dev server, so ihpLibCandidates already
covers the needed paths. The fallback candidates were duplicates.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves ihp-ide dev-mode Postgres startup reliability when running mainInParentDirectory, especially in environments where Postgres binaries (e.g. initdb) are only available via direnv-provided PATH, and fixes dev/ghci data-file resolution for IHPSchema.sql.

Changes:

  • Route Postgres setup/stale-lock commands through a direnv-aware runner when wrapWithDirenv is enabled; switch SQL imports to psql -f.
  • Fix dev/Paths_ihp_ide.getDataFileName resolution so IHPSchema.sql is found via IHP_LIB when running from a parent directory.
  • Add a regression test covering wrapped execution mode.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
ihp-ide/ihp-ide.cabal Registers the new Postgres regression spec in the test suite.
ihp-ide/Test/Main.hs Hooks the new Test.IDE.PostgresSpec into the test runner.
ihp-ide/Test/IDE/PostgresSpec.hs Adds a regression test for direnv-wrapped Postgres setup and schema loading.
ihp-ide/IHP/IDE/Postgres.hs Runs init/stop/import commands direnv-aware and uses psql -f for SQL imports.
dev/Paths_ihp_ide.hs Enhances dev-mode getDataFileName to locate IHPSchema.sql via IHP_LIB.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread ihp-ide/Test/IDE/PostgresSpec.hs Outdated
Comment on lines +69 to +71
currentDirectory <- Directory.getCurrentDirectory
let testDir = currentDirectory <> "/build/test-postgres-direnv"
ignoreIOError (Directory.removePathForcibly testDir)
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

withTemporaryTestDirectory uses a fixed path (build/test-postgres-direnv) under the repo, which can cause flaky failures if the test suite is run concurrently (or if a prior run crashed and left state behind). Prefer using a unique temp directory (e.g. via System.IO.Temp.withSystemTempDirectory) or include a random/unique suffix in the directory name.

Copilot uses AI. Check for mistakes.
Comment on lines +48 to +53
commandLines `shouldSatisfy` any ("exec . initdb " `Text.isPrefixOf`)
commandLines `shouldSatisfy` any ("exec . createdb " `Text.isPrefixOf`)
commandLines `shouldSatisfy` any ("exec . psql " `Text.isPrefixOf`)
commandLines `shouldSatisfy` any (\line -> "-f " `Text.isInfixOf` line && "IHPSchema.sql" `Text.isInfixOf` line)
commandLines `shouldSatisfy` any ("-f Application/Schema.sql" `Text.isInfixOf`)
commandLines `shouldSatisfy` any ("-f Application/Fixtures.sql" `Text.isInfixOf`)
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertions only check that the logged psql invocation contains IHPSchema.sql, but the stub psqlScript doesn’t validate that the -f path actually exists (or that it was resolved via IHP_LIB). This can let the regression test pass even if getDataFileName still returns the default relative path and the schema file can’t be found. Consider making the stub psql fail when the -f target doesn’t exist, or assert the exact expected resolved path from IHP_LIB in commands.log.

Copilot uses AI. Check for mistakes.
Comment thread ihp-ide/IHP/IDE/Postgres.hs Outdated
Comment on lines +141 to +143
if ?context.wrapWithDirenv
then Process.callProcess "direnv" (["exec", ".", commandStr] <> args)
else Process.callProcess commandStr args
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

callProcessDirenvAware duplicates the direnv-wrapping logic already implemented in procDirenvAware (in IHP.IDE.Types). Keeping two implementations increases the chance they diverge (e.g. flags/arguments/order). Consider implementing callProcessDirenvAware in terms of procDirenvAware (or moving a shared helper into IHP.IDE.Types) so all wrapped command execution follows the same code path.

Suggested change
if ?context.wrapWithDirenv
then Process.callProcess "direnv" (["exec", ".", commandStr] <> args)
else Process.callProcess commandStr args
baseProcess <- procDirenvAware commandStr args
(_, _, _, processHandle) <- Process.createProcess baseProcess
exitCode <- Process.waitForProcess processHandle
case exitCode of
ExitSuccess -> pure ()
ExitFailure c -> Exception.throwString ("Process " <> commandStr <> " exited with " <> show c)

Copilot uses AI. Check for mistakes.
amitaibu and others added 3 commits February 15, 2026 18:04
- Reimplement callProcessDirenvAware using procDirenvAware to avoid
  duplicating direnv-wrapping logic
- Use withSystemTempDirectory instead of fixed build/ path in tests
  to avoid flaky failures from leftover state
- Make psql stub verify -f file targets exist and assert exact
  IHP_LIB-resolved path for IHPSchema.sql

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The Nix build requires explicit dependencies even when transitively
available. Fixes CI failure from using System.IO.Temp in tests.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
In Nix builds, Cabal's auto-generated Paths_ihp_ide resolves to the
package data directory, not the IHP_LIB-derived path. The psql stub
validates the file exists; the assertion now just checks for an
absolute path to IHPSchema.sql.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@amitaibu
Copy link
Copy Markdown
Collaborator Author

@mpscholten ready for review

@amitaibu
Copy link
Copy Markdown
Collaborator Author

amitaibu commented Mar 3, 2026

bump

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error on mainInParentDirectory

3 participants