Repository navigation
fix(sources): resolve symlinks when computing repo-relative lock paths #365
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
Merged
Nan Liu (liunan-ms)
merged 6 commits into
microsoft:main
from
IzanVil:fix/render-symlinked-repo-path
Nov 11, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f3180bf
fix(sources): resolve symlinks when computing repo-relative lock paths
IzanVil cfe87cc
fix(component): use symlink-aware repo paths in changed and history
IzanVil d3b16cf
fix(git): follow dangling symlinks in RepoRelPath
IzanVil 74d10d2
Merge branch 'main' into fix/render-symlinked-repo-path
liunan-ms 6ddb23d
fix(component): drop unused strings import after merging main
IzanVil 39948c4
fix(git): bound symlink resolution to avoid infinite recursion on cyc…
IzanVil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,6 @@ import ( | |
| "fmt" | ||
| "path/filepath" | ||
| "sort" | ||
| "strings" | ||
|
|
||
| gogit "github.com/go-git/go-git/v5" | ||
| "github.com/go-git/go-git/v5/plumbing" | ||
|
|
@@ -249,7 +248,7 @@ func newChangedContext(env *azldev.Env) (*changedContext, error) { | |
| return nil, err | ||
| } | ||
|
|
||
| lockRelDir, err := repoRelPath(repoRoot, env.Config().Project.LockDir) | ||
| lockRelDir, err := git.RepoRelPath(repoRoot, env.Config().Project.LockDir) | ||
|
Contributor
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. Please remove the unused strings import.
liunan-ms marked this conversation as resolved.
Resolved
|
||
| if err != nil { | ||
| return nil, fmt.Errorf("computing repo-relative lock dir:\n%w", err) | ||
| } | ||
|
|
@@ -469,7 +468,7 @@ func compareSources( | |
| return false, fmt.Errorf("resolving rendered spec dir:\n%w", err) | ||
| } | ||
|
|
||
| sourcesRelPath, err := repoRelPath(repoRoot, filepath.Join(renderedDir, "sources")) | ||
| sourcesRelPath, err := git.RepoRelPath(repoRoot, filepath.Join(renderedDir, "sources")) | ||
| if err != nil { | ||
| return false, fmt.Errorf("computing repo-relative sources path:\n%w", err) | ||
| } | ||
|
|
@@ -495,21 +494,6 @@ func compareSources( | |
| } | ||
| } | ||
|
|
||
| // repoRelPath computes a repo-relative path and rejects `..`-prefixed results | ||
| // that would escape the repository root. | ||
| func repoRelPath(repoRoot, absPath string) (string, error) { | ||
| relPath, err := filepath.Rel(repoRoot, absPath) | ||
| if err != nil { | ||
| return "", fmt.Errorf("computing relative path from %#q to %#q:\n%w", repoRoot, absPath, err) | ||
| } | ||
|
|
||
| if relPath == ".." || strings.HasPrefix(relPath, ".."+string(filepath.Separator)) { | ||
| return "", fmt.Errorf("path %#q escapes repository root %#q", absPath, repoRoot) | ||
| } | ||
|
|
||
| return relPath, nil | ||
| } | ||
|
|
||
| // isFileNotFound returns true if the error indicates a missing file or | ||
| // directory in a git tree. | ||
| func isFileNotFound(err error) bool { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
internal/app/azldev/core/sources/synthistory_internal_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package sources | ||
|
|
||
| import ( | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| gogit "github.com/go-git/go-git/v5" | ||
| "github.com/go-git/go-git/v5/plumbing/object" | ||
| "github.com/microsoft/azure-linux-dev-tools/internal/global/testctx" | ||
| "github.com/microsoft/azure-linux-dev-tools/internal/projectconfig" | ||
| "github.com/microsoft/azure-linux-dev-tools/internal/utils/fileperms" | ||
| "github.com/microsoft/azure-linux-dev-tools/internal/utils/fileutils" | ||
| "github.com/spf13/afero" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestBuildSyntheticCommits_SymlinkedProjectPath(t *testing.T) { | ||
| const ( | ||
| componentName = "curl" | ||
| importCommit = "1111111111111111111111111111111111111111" | ||
| upstreamCommit = "2222222222222222222222222222222222222222" | ||
| ) | ||
|
|
||
| tempDir := t.TempDir() | ||
| realParentDir := filepath.Join(tempDir, "real") | ||
| realProjectDir := filepath.Join(realParentDir, "project") | ||
| linkedProjectDir := filepath.Join(tempDir, "link", "project") | ||
|
|
||
| osFS := afero.NewOsFs() | ||
|
|
||
| require.NoError(t, fileutils.MkdirAll(osFS, filepath.Join(realProjectDir, "locks"))) | ||
| require.NoError(t, os.Symlink(realParentDir, filepath.Join(tempDir, "link"))) | ||
|
|
||
| require.NoError(t, fileutils.WriteFile(osFS, | ||
| filepath.Join(realProjectDir, projectconfig.DefaultConfigFileName), | ||
| []byte("[components."+componentName+"]\n"), fileperms.PublicFile)) | ||
|
|
||
| require.NoError(t, fileutils.WriteFile(osFS, | ||
| filepath.Join(realProjectDir, "locks", componentName+".lock"), | ||
| []byte(strings.Join([]string{ | ||
| "version = 1", | ||
| `import-commit = "` + importCommit + `"`, | ||
| `upstream-commit = "` + upstreamCommit + `"`, | ||
| `input-fingerprint = "sha256:abc"`, | ||
| }, "\n")+"\n"), fileperms.PublicFile)) | ||
|
|
||
| repo, err := gogit.PlainInit(realProjectDir, false) | ||
| require.NoError(t, err) | ||
|
|
||
| worktree, err := repo.Worktree() | ||
| require.NoError(t, err) | ||
| require.NoError(t, worktree.AddWithOptions(&gogit.AddOptions{All: true})) | ||
|
|
||
| commitHash, err := worktree.Commit("add curl lock", &gogit.CommitOptions{ | ||
| Author: &object.Signature{ | ||
| Name: "azldev", | ||
| Email: "azldev@local", | ||
| When: time.Date(2024, 6, 1, 0, 0, 0, 0, time.UTC), | ||
| }, | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| _, config, err := projectconfig.LoadProjectConfig( | ||
| osFS, testctx.NewTestOSEnv(), linkedProjectDir, true, t.TempDir(), | ||
| nil, false, false, | ||
| ) | ||
| require.NoError(t, err) | ||
| require.Equal(t, filepath.Join(linkedProjectDir, "locks"), config.Project.LockDir, | ||
| "precondition: the loaded config keeps the unresolved path") | ||
|
|
||
| component, ok := config.Components[componentName] | ||
| require.True(t, ok) | ||
|
|
||
| var loggedPaths []string | ||
|
|
||
| cmdFactory := testctx.NewTestCmdFactory() | ||
| cmdFactory.RunAndGetOutputHandler = func(cmd *exec.Cmd) (string, error) { | ||
| loggedPaths = append(loggedPaths, cmd.Args[len(cmd.Args)-1]) | ||
|
|
||
| return strings.Join([]string{ | ||
| commitHash.String(), "azldev", "azldev@local", "1717200000", "add curl lock", | ||
| }, "\x00") + "\x01", nil | ||
| } | ||
|
|
||
| changes, gotImportCommit, err := buildSyntheticCommits( | ||
| t.Context(), cmdFactory, &component, componentName, config.Project.LockDir, "", | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| assert.Equal(t, []string{filepath.Join("locks", componentName+".lock")}, loggedPaths) | ||
| assert.Equal(t, importCommit, gotImportCommit) | ||
|
|
||
| require.Len(t, changes, 1) | ||
| assert.Equal(t, commitHash.String(), changes[0].Hash) | ||
| assert.Equal(t, upstreamCommit, changes[0].UpstreamCommit) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.