Skip to content

fix: Install-PSModules resolves wrong repo root when git rev-parse is unavailable #2697

Closed

Description

@zeier

Component

Scripts

Bug Description

Resolve-ConfigPath in scripts/security/Install-PSModules.ps1 resolves the wrong repository root whenever git rev-parse --show-toplevel does not return a value.

The fallback strips only one path segment from $PSScriptRoot, but the script lives two levels below the repository root:

$repoRoot = git rev-parse --show-toplevel 2>$null
if (-not $repoRoot) { $repoRoot = Split-Path $PSScriptRoot }
return Join-Path $repoRoot 'scripts/security/ps-module-versions.json'

$PSScriptRoot is <repo>/scripts/security, so a single Split-Path yields <repo>/scripts. Joining scripts/security/ps-module-versions.json onto that produces a duplicated segment:

<repo>/scripts/scripts/security/ps-module-versions.json

The script then fails with:

Install-PSModules failed: Config file not found: /workspaces/<repo>/scripts/scripts/security/ps-module-versions.json

The sibling script scripts/security/Test-PSModulePins.ps1 resolves the same manifest from the same directory and already handles this correctly, stripping two levels:

$repoRoot = git rev-parse --show-toplevel 2>$null
if (-not $repoRoot) {
    $repoRoot = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent
}

The two scripts disagree, which indicates a defect in Install-PSModules.ps1 rather than an intentional difference.

The fallback is reached whenever git rev-parse --show-toplevel produces no output, including:

  • a linked git worktree whose main .git directory is not reachable from the current mount
  • git not present on PATH
  • a non-clone checkout (release tarball, vendored or copied source tree)
  • git refusing to operate because of detected dubious ownership

Expected Behavior

When git rev-parse --show-toplevel is unavailable, Resolve-ConfigPath should fall back to the actual repository root and return:

<repo>/scripts/security/ps-module-versions.json

Install-PSModules.ps1 should locate the manifest and install the pinned modules, matching the behavior of Test-PSModulePins.ps1.

Steps to Reproduce

The most direct reproduction uses a linked worktree opened in the dev container, because the worktree's .git file points at the main clone's .git/worktrees/<name> directory, which is outside the container mount.

  1. From a clone of this repository, create a linked worktree and open that worktree folder in VS Code:

    git worktree add ../hve-core-wt -b repro/config-path
    code ../hve-core-wt
  2. Run Dev Containers: Rebuild Container.

  3. Observe onCreateCommand running .devcontainer/scripts/on-create.sh, which invokes:

    pwsh -NoProfile -File scripts/security/Install-PSModules.ps1 -Repository "${PSGALLERY_REPO}"
  4. Container creation fails at the Installing PowerShell modules... step with:

    Write-Error: Install-PSModules failed: Config file not found: /workspaces/<repo>/scripts/scripts/security/ps-module-versions.json
    

    The dev container log also shows git failing earlier for the same underlying reason:

    fatal: not a git repository: /workspaces/<repo>/<host-path>/.git/worktrees/<name>
    

A worktree is not required to observe the defect. The fallback can be exercised directly in any checkout:

# Simulate git being unavailable, then resolve the path
$psr = Join-Path (Get-Location) 'scripts/security'
Join-Path (Split-Path $psr) 'scripts/security/ps-module-versions.json'   # current: .../scripts/scripts/security/... 
Join-Path (Split-Path (Split-Path $psr -Parent) -Parent) 'scripts/security/ps-module-versions.json'  # expected

Additional Context

Why the existing tests do not catch this.

scripts/tests/security/Install-PSModules.Tests.ps1 covers the no-parameter, no-env-var case with:

It 'Returns a path ending in ps-module-versions.json' {
    $result = Resolve-ConfigPath -Explicit ''
    $result | Should -BeLike '*ps-module-versions.json'
}

The broken path <repo>/scripts/scripts/security/ps-module-versions.json also satisfies -BeLike '*ps-module-versions.json', so the assertion passes either way. In addition, the test runs inside a normal clone where git rev-parse succeeds, so the fallback branch is never executed. A regression test needs to force the fallback and assert on the absence of the duplicated scripts/scripts segment.

Impact.

Install-PSModules.ps1 is invoked by .devcontainer/scripts/on-create.sh, .github/actions/setup-ps-modules/action.yml, and .github/workflows/copilot-setup-steps.yml. In the dev container path, the failure is fatal to onCreateCommand, so container creation aborts and postCreateCommand is skipped. CI is unaffected in practice because git rev-parse succeeds there.

This is a latent one-line defect: in environments where git rev-parse works, the fallback is dead code and the bug is invisible.

I have a fix and a regression test ready and will open a PR referencing this issue.

Activity

  1. added
    bug
    needs-triage
    on Aug 13, 2026
  2. added a commit that references this issue on Aug 23, 2026
    e8a4b4a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugneeds-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions