-
Notifications
You must be signed in to change notification settings - Fork 691
[package-deps-hash] Fix build cache failures in git linked worktrees caused by GIT_DIR set by pre-commit hooks #5815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
beb43b9
ef318f0
4cc0538
ee1ee19
20d7b06
013803f
fb194a0
30e948e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "changes": [ | ||
| { | ||
| "packageName": "@microsoft/rush", | ||
| "comment": "Fix build cache failures when running inside a git linked worktree via a pre-commit hook, caused by GIT_DIR being set to the per-worktree metadata directory", | ||
| "type": "none" | ||
| } | ||
| ], | ||
| "packageName": "@microsoft/rush" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "changes": [ | ||
| { | ||
| "comment": "Strip GIT_DIR and GIT_WORK_TREE Node env variables to fix issues with miscalculating the git repo root when working in a linked worktree", | ||
| "type": "patch", | ||
| "packageName": "@rushstack/package-deps-hash" | ||
| } | ||
| ], | ||
| "packageName": "@rushstack/package-deps-hash", | ||
| "email": "istateside@users.noreply.github.com" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,9 +33,28 @@ const STANDARD_GIT_OPTIONS: readonly string[] = [ | |
| // `git hash-object` aborts the process. Such files are typically untracked artifacts left behind | ||
| // by tooling (e.g. stray `nul` from a shell redirect). | ||
| const WINDOWS_RESERVED_BASENAMES: ReadonlySet<string> = new Set([ | ||
| 'CON', 'PRN', 'AUX', 'NUL', | ||
| 'COM1', 'COM2', 'COM3', 'COM4', 'COM5', 'COM6', 'COM7', 'COM8', 'COM9', | ||
| 'LPT1', 'LPT2', 'LPT3', 'LPT4', 'LPT5', 'LPT6', 'LPT7', 'LPT8', 'LPT9' | ||
| 'CON', | ||
| 'PRN', | ||
| 'AUX', | ||
| 'NUL', | ||
| 'COM1', | ||
| 'COM2', | ||
| 'COM3', | ||
| 'COM4', | ||
| 'COM5', | ||
| 'COM6', | ||
| 'COM7', | ||
| 'COM8', | ||
| 'COM9', | ||
| 'LPT1', | ||
| 'LPT2', | ||
| 'LPT3', | ||
| 'LPT4', | ||
| 'LPT5', | ||
| 'LPT6', | ||
| 'LPT7', | ||
| 'LPT8', | ||
| 'LPT9' | ||
| ]); | ||
|
|
||
| /** | ||
|
|
@@ -254,6 +273,13 @@ export function parseGitStatus(output: string): Map<string, boolean> { | |
|
|
||
| const repoRootCache: Map<string, string> = new Map(); | ||
|
|
||
| // Strip GIT_DIR/GIT_WORK_TREE: git hooks in linked worktrees set GIT_DIR to the per-worktree metadata dir, causing rev-parse --show-toplevel to return CWD instead of the worktree root. | ||
| function getCleanGitEnvironment(): NodeJS.ProcessEnv { | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| const { GIT_DIR, GIT_WORK_TREE, ...trimmedEnv } = process.env; | ||
| return trimmedEnv; | ||
| } | ||
|
|
||
| /** | ||
| * Finds the root of the current Git repository | ||
| * | ||
|
|
@@ -270,7 +296,8 @@ export function getRepoRoot(currentWorkingDirectory: string, gitPath?: string): | |
| gitPath || 'git', | ||
| ['--no-optional-locks', 'rev-parse', '--show-toplevel'], | ||
| { | ||
| currentWorkingDirectory | ||
| currentWorkingDirectory, | ||
| environment: getCleanGitEnvironment() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we strip those env vars iff they point to nonexistent locations/locations without a Also note that AFAIK
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the As for when they point to nonexistent locations - that wouldn't apply in the bug case, because the GIT_DIR value points to the metadata directory, which does exist. I believe GIT_DIR is not set in the node env when you are working in the "main worktree" (i.e. you haven't created a separate worktree at all), so if that var is set at all, it should always point to a directory that does exist. In older git versions, GIT_DIR is set to the default .git location, but stripping it still forces git to navigate upwards to find the worktree root naturally, resulting in the same logic. The only users that might be incidentally affected by this would be those who are explicitly setting GIT_DIR to some unexpected value - but even in that case, I think stripping the variables is the better choice. The changed utils are all given a The way this works right now is that it strips variables that are only defined in the case that we need to fix - so stripping them unconditionally feels like the right move, even though it might appear overly aggressive. I'm open to making the change more limited if you have concerns.
To be clear, our use case is exactly that - our rush.json file lives in a subdirectory one level deeper than the repo root ( |
||
| } | ||
| ); | ||
|
|
||
|
|
@@ -305,7 +332,8 @@ async function spawnGitAsync( | |
| ): Promise<string> { | ||
| const spawnOptions: IExecutableSpawnOptions = { | ||
| currentWorkingDirectory, | ||
| stdio: ['pipe', 'pipe', 'pipe'] | ||
| stdio: ['pipe', 'pipe', 'pipe'], | ||
| environment: getCleanGitEnvironment() | ||
| }; | ||
|
|
||
| let stdout: string = ''; | ||
|
|
@@ -591,7 +619,8 @@ export function getRepoChanges( | |
| '--' | ||
| ]), | ||
| { | ||
| currentWorkingDirectory: rootDirectory | ||
| currentWorkingDirectory: rootDirectory, | ||
| environment: getCleanGitEnvironment() | ||
| } | ||
| ); | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.