Skip to content

fix: resolve \ir relative paths in single file pg_tap tests#4845

Open
7ttp wants to merge 6 commits intosupabase:developfrom
7ttp:fix/ir
Open

fix: resolve \ir relative paths in single file pg_tap tests#4845
7ttp wants to merge 6 commits intosupabase:developfrom
7ttp:fix/ir

Conversation

@7ttp
Copy link
Contributor

@7ttp 7ttp commented Feb 11, 2026

Problem

Running supabase db test <file> fails when the test file uses \ir ./other_file.sql to include other files.

$ supabase db test supabase/tests/storage_test.sql
# Error: ./testing_constants.sql: No such file or directory

The test runner passes absolute paths to pg_prove, breaking PostgreSQL's \ir relative path resolution.

Solution

Use path.Base() to pass filenames instead of absolute paths to pg_prove, while maintaining the correct working directory.

relPath := dockerPath
if path.Ext(dockerPath) != "" {
    relPath = path.Base(dockerPath)
}
cmd = append(cmd, relPath)

This allows \ir ./file.sql to resolve correctly. Directory arguments remain unchanged for backward compatibility.

Related

Summary by CodeRabbit

  • Tests
    • Enhanced test discovery to automatically resolve imported and nested test file dependencies, including multi-level traversal.
    • Added safeguards against circular imports to prevent infinite traversal.
    • Improved handling of relative test paths so test execution preserves original working-directory semantics.
    • Added unit coverage validating traversal, nested imports, and path handling.

@7ttp 7ttp requested a review from a team as a code owner February 11, 2026 07:04
@7ttp 7ttp added the bug Something isn't working label Feb 11, 2026
@7ttp 7ttp requested a review from sweatybridge February 11, 2026 07:06
Comment on lines 51 to 56
relPath := dockerPath
if path.Ext(dockerPath) != "" {
relPath = path.Base(dockerPath)
}
cmd = append(cmd, relPath)
binds[i] = fmt.Sprintf("%s:%s:ro", fp, dockerPath)
Copy link
Contributor

Choose a reason for hiding this comment

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

What if the file imported by \ir imports another file using \ir?

To support this properly, we need to traverse all the files imported. Easiest way is to use a regex expression. You can refer to this example https://github.com/supabase/cli/blob/develop/pkg/function/deno.go#L114

Copy link
Contributor

Choose a reason for hiding this comment

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

If you end up implementing that, please also write a unit test to cover these cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done 💚😅!
Added traverseImports with regex pattern matching and unit tests for nested imports, circular imports and relative paths

@coderabbitai
Copy link

coderabbitai bot commented Feb 14, 2026

📝 Walkthrough

Walkthrough

Adds recursive discovery of PostgreSQL \ir imports for test files, expands the set of files mounted into the test container, and ensures only original test files are passed to pg_prove, preserving relative path behavior for working directories.

Changes

Cohort / File(s) Summary
Import discovery & runner
internal/db/test/test.go
Adds irPattern and traverseImports to recursively resolve \ir imports, computes allFiles from provided test files, builds testFileSet to keep only original tests as pg_prove args, mounts all discovered files into Docker, and adjusts command argument construction to use relative paths when appropriate.
Tests
internal/db/test/test_test.go
Creates required test directory in setup, adds tests for Run using a nested file, and introduces TestTraverseImports with subtests for simple import, nested imports, and circular import handling.

Sequence Diagram(s)

mermaid
sequenceDiagram
participant CLI as "CLI / Runner"
participant FS as "FileSystem"
participant Resolver as "traverseImports"
participant Docker as "Docker container (mounted files)"
participant PgProve as "pg_prove (inside container)"

CLI->>FS: receive test file paths
CLI->>Resolver: request import traversal
Resolver->>FS: read files, parse \ir directives
Resolver-->>Resolver: resolve nested/circular imports
Resolver-->>CLI: return expanded file list (allFiles)
CLI->>Docker: mount allFiles as binds
CLI->>PgProve: pass only original test files (relative paths when applicable)
Docker->>PgProve: execute tests
PgProve-->>CLI: return results

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 2
❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Merge Conflict Detection ⚠️ Warning ❌ Merge conflicts detected (23 files):

⚔️ cmd/branches.go (content)
⚔️ cmd/db.go (content)
⚔️ cmd/encryption.go (content)
⚔️ cmd/gen.go (content)
⚔️ go.mod (content)
⚔️ go.sum (content)
⚔️ internal/branches/create/create.go (content)
⚔️ internal/config/push/push_test.go (content)
⚔️ internal/db/diff/diff.go (content)
⚔️ internal/db/reset/reset.go (content)
⚔️ internal/db/test/test.go (content)
⚔️ internal/db/test/test_test.go (content)
⚔️ internal/encryption/get/get.go (content)
⚔️ internal/encryption/get/get_test.go (content)
⚔️ internal/encryption/update/update.go (content)
⚔️ internal/encryption/update/update_test.go (content)
⚔️ internal/functions/list/list.go (content)
⚔️ internal/functions/list/list_test.go (content)
⚔️ internal/gen/signingkeys/signingkeys.go (content)
⚔️ internal/gen/signingkeys/signingkeys_test.go (content)
⚔️ pkg/config/templates/Dockerfile (content)
⚔️ pkg/go.mod (content)
⚔️ pkg/go.sum (content)

These conflicts must be resolved before merging into develop.
Resolve conflicts locally and push changes to this branch.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main fix: resolving \ir relative paths in single file pg_tap tests by passing basenames instead of absolute paths.
Linked Issues check ✅ Passed The changes directly address issue #4850 by implementing recursive import discovery and passing basenames to pg_prove to allow \ir relative path resolution.
Out of Scope Changes check ✅ Passed All changes are scoped to fixing the \ir relative path resolution issue in single file test runs, with no unrelated modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
⚔️ Resolve merge conflicts (beta)
  • Auto-commit resolved conflicts to branch fix/ir
  • Post resolved changes as copyable diffs in a comment

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Fix all issues with AI agents
In `@internal/db/test/test.go`:
- Line 30: The regex in var irPattern currently anchors to line start so
indented \ir directives are missed; update the regular expression in var
irPattern (the compiled pattern used to find `\ir` directives) to allow optional
leading whitespace before the backslash (e.g. add `\s*` after the `^`), keeping
the rest of the capture group intact so filenames are still captured the same
way.
- Around line 34-66: The multi-dir failure is caused by always reducing
dockerPath to path.Base for test files; change the logic in the loop over
allFiles (the block that sets workingDir, computes dockerPath, and appends to
cmd/binds) so that path.Base(dockerPath) is used only when the directory of
dockerPath equals the chosen workingDir (which is set from the first file); for
files from other directories keep the full dockerPath when appending to cmd (so
pg_prove can find them) and still add the corresponding binds entry; update the
code around traverseImports, testFiles, allFiles, cmd, workingDir, and binds to
reflect this conditional basename behavior.
🧹 Nitpick comments (1)
internal/db/test/test_test.go (1)

107-141: Strengthen traversal assertions to validate file names, not just counts.
Right now, Line 116/128/139 only checks length, so a wrong path could still pass. Consider asserting the expected file set.

💡 Example tightening for all three subtests
-        assert.Len(t, result, 2)
+        assert.ElementsMatch(t, []string{"main.sql", "helper.sql"}, result)
...
-        assert.Len(t, result, 3)
+        assert.ElementsMatch(t, []string{"main.sql", "level1.sql", "level2.sql"}, result)
...
-        assert.Len(t, result, 2)
+        assert.ElementsMatch(t, []string{"a.sql", "b.sql"}, result)

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@internal/db/test/test.go`:
- Around line 125-155: traverseImports currently uses q as a LIFO stack which
reverses testFiles order; change traversal to FIFO so imports are processed in
input order (preserve order of testFiles in result). Modify the loop in
traverseImports to consume q from the front (e.g., iterate with an index over q
or dequeue from the head) instead of popping the last element, keep the same
logic for marking seen, reading files, and appending discovered imports to q so
the final result order remains stable for Run and pg_prove behavior.

@7ttp 7ttp requested a review from sweatybridge February 14, 2026 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

no longer possible to run a single test file when it includes another

2 participants