Skip to content

Replant symlinks before caching repos - #28582

Closed
fmeum wants to merge 1 commit into
bazelbuild:masterfrom
fmeum:28575-replant-symlinks
Closed

fmeum wants to merge 1 commit into
bazelbuild:masterfrom
fmeum:28575-replant-symlinks

Conversation

@fmeum

@fmeum fmeum commented Feb 9, 2026

Copy link
Copy Markdown
Collaborator

This reuses the logic for replanting symlinks in the vendor directory, which is improved to also cover the case of symlinks into the main repository.

Work towards #28575

@fmeum
fmeum force-pushed the 28575-replant-symlinks branch 4 times, most recently from 56a60d3 to efad1a0 Compare February 10, 2026 13:41
@iancha1992

iancha1992 commented Feb 13, 2026 •

Copy link
Copy Markdown
Contributor

Hi @fmeum. Could you please take a look at the test failure? We're hoping we could include the fix for #28575 in our 9.0.1 RC1 on Tuesday.

@fmeum

fmeum commented Feb 14, 2026 •

Copy link
Copy Markdown
Collaborator Author

I'll try to figure this out by then. Windows is proving tricky.

@fmeum
fmeum force-pushed the 28575-replant-symlinks branch 4 times, most recently from 1e6e2a4 to 0c281a6 Compare February 17, 2026 11:09
@fmeum
fmeum force-pushed the 28575-replant-symlinks branch from 0c281a6 to 21b2abf Compare February 17, 2026 13:33
@fmeum
fmeum marked this pull request as ready for review February 17, 2026 13:33
@github-actions github-actions Bot added team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. awaiting-review PR is awaiting review from an assigned reviewer labels Feb 17, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

The pull request refactors the symlink replanting logic into a shared utility function RepositoryUtils.replantSymlinks and applies it consistently across vendoring and repository caching mechanisms. This change improves the portability of cached repositories by converting absolute symlinks to relative paths. Additionally, it introduces a new SYMLINKING_FAILED error code for better failure reporting and updates relevant test cases to reflect the new symlink handling.

@fmeum

fmeum commented Feb 17, 2026

Copy link
Copy Markdown
Collaborator Author

@meteorcloudy I've seen Gemini go off the rails like this a few times - it writes a comment for every part of the code.

@meteorcloudy

Copy link
Copy Markdown
Member

Sign, I'll change comment_severity_threshold to CRITICAL

copybara-service Bot pushed a commit that referenced this pull request Feb 18, 2026
Hopefully reduce noise such as #28582

PiperOrigin-RevId: 871764353
Change-Id: I89a2fa2d269263486d98e011ff4c73cb388b17c3
@iancha1992

Copy link
Copy Markdown
Contributor

@meteorcloudy Is this ready for merge?

@meteorcloudy meteorcloudy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice, thanks!

@meteorcloudy meteorcloudy added awaiting-PR-merge PR has been approved by a reviewer and is ready to be merge internally and removed awaiting-review PR is awaiting review from an assigned reviewer labels Feb 23, 2026
@meteorcloudy

Copy link
Copy Markdown
Member

@iancha1992 Yes, please import

copybara-service Bot pushed a commit to bazelbuild/intellij that referenced this pull request Feb 24, 2026
This reuses the logic for replanting symlinks in the vendor directory, which is improved to also cover the case of symlinks into the main repository.

Work towards #28575

Closes #28582.

PiperOrigin-RevId: 874701377
@iancha1992

Copy link
Copy Markdown
Contributor

@bazel-io fork 9.0.1

@iancha1992

Copy link
Copy Markdown
Contributor

@bazel-io fork 9.1.0

@github-actions github-actions Bot removed the awaiting-PR-merge PR has been approved by a reviewer and is ready to be merge internally label Feb 24, 2026
bazel-io pushed a commit to bazel-io/bazel that referenced this pull request Feb 24, 2026
This reuses the logic for replanting symlinks in the vendor directory, which is improved to also cover the case of symlinks into the main repository.

Work towards bazelbuild#28575

Closes bazelbuild#28582.

PiperOrigin-RevId: 874701377
Change-Id: I661166dc8102421e62f9a4196bd0d305e0ec2bb7
bazel-io pushed a commit to bazel-io/bazel that referenced this pull request Feb 24, 2026
This reuses the logic for replanting symlinks in the vendor directory, which is improved to also cover the case of symlinks into the main repository.

Work towards bazelbuild#28575

Closes bazelbuild#28582.

PiperOrigin-RevId: 874701377
Change-Id: I661166dc8102421e62f9a4196bd0d305e0ec2bb7
github-merge-queue Bot pushed a commit that referenced this pull request Feb 24, 2026
This reuses the logic for replanting symlinks in the vendor directory,
which is improved to also cover the case of symlinks into the main
repository.

Work towards #28575

Closes #28582.

PiperOrigin-RevId: 874701377
Change-Id: I661166dc8102421e62f9a4196bd0d305e0ec2bb7

Commit
2ce32e7

---------

Co-authored-by: Fabian Meumertzheim <[email protected]>
Co-authored-by: Ian (Hee) Cha <[email protected]>
github-merge-queue Bot pushed a commit that referenced this pull request Feb 25, 2026
This reuses the logic for replanting symlinks in the vendor directory,
which is improved to also cover the case of symlinks into the main
repository.

Work towards #28575

Closes #28582.

PiperOrigin-RevId: 874701377
Change-Id: I661166dc8102421e62f9a4196bd0d305e0ec2bb7

Commit
2ce32e7

---------

Co-authored-by: Fabian Meumertzheim <[email protected]>
Co-authored-by: Ian (Hee) Cha <[email protected]>
@fmeum
fmeum deleted the 28575-replant-symlinks branch March 30, 2026 19:03
fmeum added a commit to fmeum/bazel that referenced this pull request Jun 26, 2026
When a remotely cached repo is partially materialized since it contains action
inputs, Skyframe provides the metadata of these inputs after following all
symlinks as it does for all source files. Since (chains of) symlinks need to be
preserved and materialized in the same way as if the repo rule ran locally or
was fully materialized as a dependency of another repo rule,
`AbstractActionInputPrefetcher` is changed to walk the full chain of symlinks
behind a prefetched external repo path and replant every link verbatim on the
host file system.

This proves very difficult to get right for repos with symlinks pointing into
the main repo as such symlinks would cross over into the host filesystem. For
this reason such symlinks now exclude a repo from being cached remotely. Note
that the local repo contents cache already doesn't cache such repos. Since main
repo files are readily available, this is considered a minor loss in usefulness
only.

8.x adaptation: the 8.x prefetcher uses an exec-path-based symlink model
(`resolveExecPath`, `execRoot()`, `FileArtifactValue#getMaterializationExecPath`)
rather than the master model (`getResolvedPath`, `execRoot` field). The
chain-walking is therefore re-expressed on that model: the `Symlink` record
becomes `(Path linkPath, PathFragment targetPath)` as upstream, but the
materialization case resolves `getMaterializationExecPath()` through
`resolveExecPath` instead of reading `getResolvedPath()`; `execRoot` field
accesses become `execRoot()` calls; the loop guard throws
`FileSymlinkLoopException(PathFragment)` (the 8.x constructor); and the 8.x
`CacheNotFoundException` rewinding `onErrorResumeNext` is preserved.

On the repo side, 8.x has no `RepositoryUtils` or merged
`RepositoryFetchFunction`, so the `ReplantSymlinksResult` record and the
main-repo-symlink exclusion are applied to `RepositoryFunction.replantSymlinks`
and the `RepositoryDelegatorFunction` caching path (where remote caching is now
gated on `safeForRemoteCache()` and local caching on `safeForLocalCache()`).
`VendorManager` keeps its existing private `replantSymlinks`, so the upstream
call-signature change there does not apply.

Depends on the replant prerequisite (bazelbuild#28582) backported in the preceding slot.

Fixes bazelbuild#29656.
Fixes bazelbuild#29515.

Closes bazelbuild#29767.

PiperOrigin-RevId: 936532134
Change-Id: I48b63f6fd2d5f57e139175e3ed578d65f6abbd08
(cherry picked from commit c310e3e)
fmeum added a commit to fmeum/bazel that referenced this pull request Jun 26, 2026
When a remotely cached repo is partially materialized since it contains action
inputs, Skyframe provides the metadata of these inputs after following all
symlinks as it does for all source files. Since (chains of) symlinks need to be
preserved and materialized in the same way as if the repo rule ran locally or
was fully materialized as a dependency of another repo rule,
`AbstractActionInputPrefetcher` is changed to walk the full chain of symlinks
behind a prefetched external repo path and replant every link verbatim on the
host file system.

This proves very difficult to get right for repos with symlinks pointing into
the main repo as such symlinks would cross over into the host filesystem. For
this reason such symlinks now exclude a repo from being cached remotely. Note
that the local repo contents cache already doesn't cache such repos. Since main
repo files are readily available, this is considered a minor loss in usefulness
only.

8.x adaptation: the 8.x prefetcher uses an exec-path-based symlink model
(`resolveExecPath`, `execRoot()`, `FileArtifactValue#getMaterializationExecPath`)
rather than the master model (`getResolvedPath`, `execRoot` field). The
chain-walking is therefore re-expressed on that model: the `Symlink` record
becomes `(Path linkPath, PathFragment targetPath)` as upstream, but the
materialization case resolves `getMaterializationExecPath()` through
`resolveExecPath` instead of reading `getResolvedPath()`; the loop guard throws
`FileSymlinkLoopException(PathFragment)` (the 8.x constructor); and the 8.x
`CacheNotFoundException` rewinding `onErrorResumeNext` is preserved. Because the
exec root isn't available during external repository materialization, both
`getSymlinks` and `plantSymlink` detect external repo paths via the
`isUnderExternalRepoRoot` helper (introduced in the bazelbuild#28683 backport) instead of
`execRoot()`, which would otherwise NPE during materialization.

On the repo side, 8.x has no `RepositoryUtils` or merged
`RepositoryFetchFunction`, so the `ReplantSymlinksResult` record and the
main-repo-symlink exclusion are applied to `RepositoryFunction.replantSymlinks`
and the `RepositoryDelegatorFunction` caching path (where remote caching is now
gated on `safeForRemoteCache()` and local caching on `safeForLocalCache()`).
`VendorManager` keeps its existing private `replantSymlinks`, so the upstream
call-signature change there does not apply.

This commit also carries the three symlink-materialization tests added upstream
in bazelbuild#28683: on 8.x they only pass once this chain-walk change is in place, since
repo files are materialized lazily through `RemoteExternalOverlayFileSystem`
rather than fully on disk as on master.

Depends on the replant prerequisite (bazelbuild#28582) backported in the preceding slot.

Fixes bazelbuild#29656.
Fixes bazelbuild#29515.

Closes bazelbuild#29767.

PiperOrigin-RevId: 936532134
Change-Id: I48b63f6fd2d5f57e139175e3ed578d65f6abbd08
(cherry picked from commit c310e3e)
fmeum added a commit to fmeum/bazel that referenced this pull request Jul 1, 2026
This reuses the logic for replanting symlinks in the vendor directory, which is
improved to also cover the case of symlinks into the main repository. Before a
reproducible, non-local repo is added to the local or remote repo contents
cache, its symlinks are rewritten from absolute paths (pointing into the
workspace or external root) to relative paths, making the cached repo portable
across output bases and machines. A repo with cross-repo symlinks is not safe to
reuse from a shared local cache location and is therefore excluded from the
local repo contents cache.

8.x adaptation: 8.x has no `RepositoryUtils` class and no merged
`RepositoryFetchFunction` worker. The shared `replantSymlinks` helper (and the
`WORKSPACE_SYMLINK_NAME` constant) is therefore added to `RepositoryFunction` in
`rules/repository`, the lowest layer reachable from the caching code in
`RepositoryDelegatorFunction`. It can't live in a `bazel/repository` or `bzlmod`
utility like upstream because `rules/repository` already depends on `bzlmod`
(for `BzlmodRepoRuleValue`/`VendorFileValue`), so a back-dependency would create
a cycle. For the same reason the upstream refactor that routes `VendorManager`
through the shared helper is dropped; `VendorManager` keeps its existing private
`replantSymlinks`. The replant call and the `safeForLocalCacheReuse` gating are
added to the `RepositoryDelegatorFunction` fetch path instead of
`RepositoryFetchFunction`. The `@CanIgnoreReturnValue` annotation is dropped
because the only 8.x caller uses the result, which avoids adding an
`error_prone_annotations` build dependency.

This commit was identified as a missing prerequisite for the symlink-chain
materialization change (bazelbuild#29767) and is not part of the original cherry-pick list
in bazelbuild#29031.

Work towards bazelbuild#28575.

Closes bazelbuild#28582.

PiperOrigin-RevId: 874701377
Change-Id: I661166dc8102421e62f9a4196bd0d305e0ec2bb7
(cherry picked from commit 2ce32e7)
fmeum added a commit to fmeum/bazel that referenced this pull request Jul 1, 2026
This reuses the logic for replanting symlinks in the vendor directory, which is
improved to also cover the case of symlinks into the main repository. Before a
reproducible, non-local repo is added to the local or remote repo contents
cache, its symlinks are rewritten from absolute paths (pointing into the
workspace or external root) to relative paths, making the cached repo portable
across output bases and machines. A repo with cross-repo symlinks is not safe to
reuse from a shared local cache location and is therefore excluded from the
local repo contents cache.

8.x adaptation: 8.x has no `RepositoryUtils` class and no merged
`RepositoryFetchFunction` worker. The shared `replantSymlinks` helper (and the
`WORKSPACE_SYMLINK_NAME` constant) is therefore added to `RepositoryFunction` in
`rules/repository`, the lowest layer reachable from the caching code in
`RepositoryDelegatorFunction`. It can't live in a `bazel/repository` or `bzlmod`
utility like upstream because `rules/repository` already depends on `bzlmod`
(for `BzlmodRepoRuleValue`/`VendorFileValue`), so a back-dependency would create
a cycle. For the same reason the upstream refactor that routes `VendorManager`
through the shared helper is dropped; `VendorManager` keeps its existing private
`replantSymlinks`. The replant call and the `safeForLocalCacheReuse` gating are
added to the `RepositoryDelegatorFunction` fetch path instead of
`RepositoryFetchFunction`. The `@CanIgnoreReturnValue` annotation is dropped
because the only 8.x caller uses the result, which avoids adding an
`error_prone_annotations` build dependency.

This commit was identified as a missing prerequisite for the symlink-chain
materialization change (bazelbuild#29767) and is not part of the original cherry-pick list
in bazelbuild#29031.

Work towards bazelbuild#28575.

Closes bazelbuild#28582.

PiperOrigin-RevId: 874701377
Change-Id: I661166dc8102421e62f9a4196bd0d305e0ec2bb7
(cherry picked from commit 2ce32e7)
fmeum added a commit to fmeum/bazel that referenced this pull request Jul 8, 2026
This reuses the logic for replanting symlinks in the vendor directory, which is
improved to also cover the case of symlinks into the main repository. Before a
reproducible, non-local repo is added to the local or remote repo contents
cache, its symlinks are rewritten from absolute paths (pointing into the
workspace or external root) to relative paths, making the cached repo portable
across output bases and machines. A repo with cross-repo symlinks is not safe to
reuse from a shared local cache location and is therefore excluded from the
local repo contents cache.

8.x adaptation: 8.x has no `RepositoryUtils` class and no merged
`RepositoryFetchFunction` worker. The shared `replantSymlinks` helper (and the
`WORKSPACE_SYMLINK_NAME` constant) is therefore added to `RepositoryFunction` in
`rules/repository`, the lowest layer reachable from the caching code in
`RepositoryDelegatorFunction`. It can't live in a `bazel/repository` or `bzlmod`
utility like upstream because `rules/repository` already depends on `bzlmod`
(for `BzlmodRepoRuleValue`/`VendorFileValue`), so a back-dependency would create
a cycle. For the same reason the upstream refactor that routes `VendorManager`
through the shared helper is dropped; `VendorManager` keeps its existing private
`replantSymlinks`. The replant call and the `safeForLocalCacheReuse` gating are
added to the `RepositoryDelegatorFunction` fetch path instead of
`RepositoryFetchFunction`. The `@CanIgnoreReturnValue` annotation is dropped
because the only 8.x caller uses the result, which avoids adding an
`error_prone_annotations` build dependency.

This commit was identified as a missing prerequisite for the symlink-chain
materialization change (bazelbuild#29767) and is not part of the original cherry-pick list
in bazelbuild#29031.

Work towards bazelbuild#28575.

Closes bazelbuild#28582.

PiperOrigin-RevId: 874701377
Change-Id: I661166dc8102421e62f9a4196bd0d305e0ec2bb7
(cherry picked from commit 2ce32e7)
fmeum added a commit to fmeum/bazel that referenced this pull request Jul 9, 2026
When using the remote repo contents cache, repo materialization now uses
the same code path as local action input prefetching for symlinks. This
ensures that each symlink is only materialized once and that there are no
races in case symlinking is not atomic (on Windows with default settings).
Along the way, remove unreachable code from `UploadManifest`.

Furthermore, when a remotely cached repo is partially materialized since it
contains action inputs, Skyframe provides the metadata of these inputs
after following all symlinks as it does for all source files. Since (chains
of) symlinks need to be preserved and materialized in the same way as if
the repo rule ran locally or was fully materialized as a dependency of
another repo rule, `AbstractActionInputPrefetcher` is changed to walk the
full chain of symlinks behind a prefetched external repo path and replant
every link verbatim on the host file system.

This proves very difficult to get right for repos with symlinks pointing
into the main repo as such symlinks would cross over into the host
filesystem. For this reason such symlinks now exclude a repo from being
cached remotely. Note that the local repo contents cache already doesn't
cache such repos. Since main repo files are readily available, this is
considered a minor loss in usefulness only.

This commit combines the backports of bazelbuild#28683 and bazelbuild#29767 into one: the
symlink-materialization tests added upstream in bazelbuild#28683 only pass on 8.x
once the chain-walk change from bazelbuild#29767 is in place, since repo files are
materialized lazily through `RemoteExternalOverlayFileSystem` rather than
fully on disk as on master.

8.x adaptation: `AbstractActionInputPrefetcher` on 8.x resolves input paths
via the Bazel 8.x-specific `resolveExecPath(execPath)` helper (which avoids
evaluating the WORKSPACE-named exec root when possible) and exposes the
exec root through the `execRoot()` method rather than an `execRoot` field,
so the new symlink handling is expressed in those terms. Because
`execRoot()` is not available during external repository materialization,
external repo paths are detected via a new `isUnderExternalRepoRoot`
helper (mirroring `DirectoryTracker.setWritable`) instead of
`!inputPath.startsWith(execRoot())`; both `getSymlinks` and `plantSymlink`
use it. The chain-walking is re-expressed on the exec-path-based symlink
model (`resolveExecPath`, `execRoot()`,
`FileArtifactValue#getMaterializationExecPath`) rather than the master
model (`getResolvedPath`, `execRoot` field): the `Symlink` record becomes
`(Path linkPath, PathFragment targetPath)` as upstream, but the
materialization case resolves `getMaterializationExecPath()` through
`resolveExecPath` instead of reading `getResolvedPath()`; the loop guard
throws `FileSymlinkLoopException(PathFragment)` (the 8.x constructor); the
symlink target of an unresolved symlink artifact is read from
`UnresolvedSymlinkArtifactValue#getSymlinkTarget` (8.x has no
`FileArtifactValue#getUnresolvedSymlinkTarget`); and the 8.x
`CacheNotFoundException` rewinding `onErrorResumeNext` is preserved.

On the repo side, 8.x has no `RepositoryUtils` or merged
`RepositoryFetchFunction`, so the `ReplantSymlinksResult` record and the
main-repo-symlink exclusion are applied to
`RepositoryFunction.replantSymlinks` and the `RepositoryDelegatorFunction`
caching path (where remote caching is now gated on `safeForRemoteCache()`
and local caching on `safeForLocalCache()`). `VendorManager` keeps its
existing private `replantSymlinks`, so the upstream call-signature change
there does not apply.

Depends on the replant prerequisite (bazelbuild#28582) backported in the preceding
slot.

Fixes bazelbuild#28575.
Fixes bazelbuild#29656.
Fixes bazelbuild#29515.

Closes bazelbuild#28683.

PiperOrigin-RevId: 893359962
Change-Id: If0eb64e41858a4b9298c2609d83160b0ecbfd451
(cherry picked from commit 90eb56e)

Closes bazelbuild#29767.

PiperOrigin-RevId: 936532134
Change-Id: I48b63f6fd2d5f57e139175e3ed578d65f6abbd08
(cherry picked from commit c310e3e)
fmeum added a commit to fmeum/bazel that referenced this pull request Jul 12, 2026
This reuses the logic for replanting symlinks in the vendor directory, which is
improved to also cover the case of symlinks into the main repository. Before a
reproducible, non-local repo is added to the local or remote repo contents
cache, its symlinks are rewritten from absolute paths (pointing into the
workspace or external root) to relative paths, making the cached repo portable
across output bases and machines. A repo with cross-repo symlinks is not safe to
reuse from a shared local cache location and is therefore excluded from the
local repo contents cache.

8.x adaptation: 8.x has no `RepositoryUtils` class and no merged
`RepositoryFetchFunction` worker. The shared `replantSymlinks` helper (and the
`WORKSPACE_SYMLINK_NAME` constant) is therefore added to `RepositoryFunction` in
`rules/repository`, the lowest layer reachable from the caching code in
`RepositoryDelegatorFunction`. It can't live in a `bazel/repository` or `bzlmod`
utility like upstream because `rules/repository` already depends on `bzlmod`
(for `BzlmodRepoRuleValue`/`VendorFileValue`), so a back-dependency would create
a cycle. For the same reason the upstream refactor that routes `VendorManager`
through the shared helper is dropped; `VendorManager` keeps its existing private
`replantSymlinks`. The replant call and the `safeForLocalCacheReuse` gating are
added to the `RepositoryDelegatorFunction` fetch path instead of
`RepositoryFetchFunction`. The `@CanIgnoreReturnValue` annotation is dropped
because the only 8.x caller uses the result, which avoids adding an
`error_prone_annotations` build dependency.

On Windows, `replantSymlinks` obtains NIO paths from the file system to create
symlinks with relative targets and expects `FileSystem#getNioPath` to return
null for file systems not backed by the local file system. This requires two
more pieces present on master but missing on 8.x: `FileSystem#getIoFile` and
`FileSystem#getNioPath` return null instead of throwing
`UnsupportedOperationException` (from commit
ec90e05) and
`RemoteExternalOverlayFileSystem` gains the delegating overrides for these two
methods that master's version has had since commit
b8589c3b2792bd83a3d29ed653a86d1729906ffb. Without them, replanting a freshly
fetched repo crashes on Windows when the remote repo contents cache is enabled.

This commit was identified as a missing prerequisite for the symlink-chain
materialization change (bazelbuild#29767) and is not part of the original cherry-pick list
in bazelbuild#29031.

Work towards bazelbuild#28575.

Closes bazelbuild#28582.

PiperOrigin-RevId: 874701377
Change-Id: I661166dc8102421e62f9a4196bd0d305e0ec2bb7
(cherry picked from commit 2ce32e7)
fmeum added a commit to fmeum/bazel that referenced this pull request Jul 12, 2026
When using the remote repo contents cache, repo materialization now uses
the same code path as local action input prefetching for symlinks. This
ensures that each symlink is only materialized once and that there are no
races in case symlinking is not atomic (on Windows with default settings).
Along the way, remove unreachable code from `UploadManifest`.

Furthermore, when a remotely cached repo is partially materialized since it
contains action inputs, Skyframe provides the metadata of these inputs
after following all symlinks as it does for all source files. Since (chains
of) symlinks need to be preserved and materialized in the same way as if
the repo rule ran locally or was fully materialized as a dependency of
another repo rule, `AbstractActionInputPrefetcher` is changed to walk the
full chain of symlinks behind a prefetched external repo path and replant
every link verbatim on the host file system.

This proves very difficult to get right for repos with symlinks pointing
into the main repo as such symlinks would cross over into the host
filesystem. For this reason such symlinks now exclude a repo from being
cached remotely. Note that the local repo contents cache already doesn't
cache such repos. Since main repo files are readily available, this is
considered a minor loss in usefulness only.

This commit combines the backports of bazelbuild#28683 and bazelbuild#29767 into one: the
symlink-materialization tests added upstream in bazelbuild#28683 only pass on 8.x
once the chain-walk change from bazelbuild#29767 is in place, since repo files are
materialized lazily through `RemoteExternalOverlayFileSystem` rather than
fully on disk as on master.

8.x adaptation: `AbstractActionInputPrefetcher` on 8.x resolves input paths
via the Bazel 8.x-specific `resolveExecPath(execPath)` helper (which avoids
evaluating the WORKSPACE-named exec root when possible) and exposes the
exec root through the `execRoot()` method rather than an `execRoot` field,
so the new symlink handling is expressed in those terms. Because
`execRoot()` is not available during external repository materialization,
external repo paths are detected via a new `isUnderExternalRepoRoot`
helper (mirroring `DirectoryTracker.setWritable`) instead of
`!inputPath.startsWith(execRoot())`; both `getSymlinks` and `plantSymlink`
use it. The chain-walking is re-expressed on the exec-path-based symlink
model (`resolveExecPath`, `execRoot()`,
`FileArtifactValue#getMaterializationExecPath`) rather than the master
model (`getResolvedPath`, `execRoot` field): the `Symlink` record becomes
`(Path linkPath, PathFragment targetPath)` as upstream, but the
materialization case resolves `getMaterializationExecPath()` through
`resolveExecPath` instead of reading `getResolvedPath()`; the loop guard
throws `FileSymlinkLoopException(PathFragment)` (the 8.x constructor); the
symlink target of an unresolved symlink artifact is read from
`UnresolvedSymlinkArtifactValue#getSymlinkTarget` (8.x has no
`FileArtifactValue#getUnresolvedSymlinkTarget`); and the 8.x
`CacheNotFoundException` rewinding `onErrorResumeNext` is preserved.

On the repo side, 8.x has no `RepositoryUtils` or merged
`RepositoryFetchFunction`, so the `ReplantSymlinksResult` record and the
main-repo-symlink exclusion are applied to
`RepositoryFunction.replantSymlinks` and the `RepositoryDelegatorFunction`
caching path (where remote caching is now gated on `safeForRemoteCache()`
and local caching on `safeForLocalCache()`). `VendorManager` keeps its
existing private `replantSymlinks`, so the upstream call-signature change
there does not apply.

Depends on the replant prerequisite (bazelbuild#28582) backported in the preceding
slot.

Fixes bazelbuild#28575.
Fixes bazelbuild#29656.
Fixes bazelbuild#29515.

Closes bazelbuild#28683.

PiperOrigin-RevId: 893359962
Change-Id: If0eb64e41858a4b9298c2609d83160b0ecbfd451
(cherry picked from commit 90eb56e)

Closes bazelbuild#29767.

PiperOrigin-RevId: 936532134
Change-Id: I48b63f6fd2d5f57e139175e3ed578d65f6abbd08
(cherry picked from commit c310e3e)
iancha1992 pushed a commit to iancha1992/bazel that referenced this pull request Jul 13, 2026
This reuses the logic for replanting symlinks in the vendor directory,
which is improved to also cover the case of symlinks into the main
repository. Before a reproducible, non-local repo is added to the local
or remote repo contents cache, its symlinks are rewritten from absolute
paths (pointing into the workspace or external root) to relative paths,
making the cached repo portable across output bases and machines. A repo
with cross-repo symlinks is not safe to reuse from a shared local cache
location and is therefore excluded from the local repo contents cache.

8.x adaptation: 8.x has no `RepositoryUtils` class and no merged
`RepositoryFetchFunction` worker. The shared `replantSymlinks` helper
(and the `WORKSPACE_SYMLINK_NAME` constant) is therefore added to
`RepositoryFunction` in `rules/repository`, the lowest layer reachable
from the caching code in `RepositoryDelegatorFunction`. It can't live in
a `bazel/repository` or `bzlmod` utility like upstream because
`rules/repository` already depends on `bzlmod` (for
`BzlmodRepoRuleValue`/`VendorFileValue`), so a back-dependency would
create a cycle. For the same reason the upstream refactor that routes
`VendorManager` through the shared helper is dropped; `VendorManager`
keeps its existing private `replantSymlinks`. The replant call and the
`safeForLocalCacheReuse` gating are added to the
`RepositoryDelegatorFunction` fetch path instead of
`RepositoryFetchFunction`. The `@CanIgnoreReturnValue` annotation is
dropped because the only 8.x caller uses the result, which avoids adding
an `error_prone_annotations` build dependency.

On Windows, `replantSymlinks` obtains NIO paths from the file system to
create symlinks with relative targets and expects
`FileSystem#getNioPath` to return null for file systems not backed by
the local file system. This requires two more pieces present on master
but missing on 8.x: `FileSystem#getIoFile` and `FileSystem#getNioPath`
return null instead of throwing `UnsupportedOperationException` (from
commit ec90e05) and
`RemoteExternalOverlayFileSystem` gains the delegating overrides for
these two methods that master's version has had since commit
b8589c3b2792bd83a3d29ed653a86d1729906ffb. Without them, replanting a
freshly fetched repo crashes on Windows when the remote repo contents
cache is enabled.

Work towards bazelbuild#28575.

Closes bazelbuild#28582.

PiperOrigin-RevId: 874701377
Change-Id: I661166dc8102421e62f9a4196bd0d305e0ec2bb7
(cherry picked from commit 2ce32e7)
fmeum added a commit to fmeum/bazel that referenced this pull request Jul 13, 2026
When using the remote repo contents cache, repo materialization now uses
the same code path as local action input prefetching for symlinks. This
ensures that each symlink is only materialized once and that there are no
races in case symlinking is not atomic (on Windows with default settings).
Along the way, remove unreachable code from `UploadManifest`.

Furthermore, when a remotely cached repo is partially materialized since it
contains action inputs, Skyframe provides the metadata of these inputs
after following all symlinks as it does for all source files. Since (chains
of) symlinks need to be preserved and materialized in the same way as if
the repo rule ran locally or was fully materialized as a dependency of
another repo rule, `AbstractActionInputPrefetcher` is changed to walk the
full chain of symlinks behind a prefetched external repo path and replant
every link verbatim on the host file system.

This proves very difficult to get right for repos with symlinks pointing
into the main repo as such symlinks would cross over into the host
filesystem. For this reason such symlinks now exclude a repo from being
cached remotely. Note that the local repo contents cache already doesn't
cache such repos. Since main repo files are readily available, this is
considered a minor loss in usefulness only.

This commit combines the backports of bazelbuild#28683 and bazelbuild#29767 into one: the
symlink-materialization tests added upstream in bazelbuild#28683 only pass on 8.x
once the chain-walk change from bazelbuild#29767 is in place, since repo files are
materialized lazily through `RemoteExternalOverlayFileSystem` rather than
fully on disk as on master.

8.x adaptation: `AbstractActionInputPrefetcher` on 8.x resolves input paths
via the Bazel 8.x-specific `resolveExecPath(execPath)` helper (which avoids
evaluating the WORKSPACE-named exec root when possible) and exposes the
exec root through the `execRoot()` method rather than an `execRoot` field,
so the new symlink handling is expressed in those terms. Because
`execRoot()` is not available during external repository materialization,
external repo paths are detected via a new `isUnderExternalRepoRoot`
helper (mirroring `DirectoryTracker.setWritable`) instead of
`!inputPath.startsWith(execRoot())`; both `getSymlinks` and `plantSymlink`
use it. The chain-walking is re-expressed on the exec-path-based symlink
model (`resolveExecPath`, `execRoot()`,
`FileArtifactValue#getMaterializationExecPath`) rather than the master
model (`getResolvedPath`, `execRoot` field): the `Symlink` record becomes
`(Path linkPath, PathFragment targetPath)` as upstream, but the
materialization case resolves `getMaterializationExecPath()` through
`resolveExecPath` instead of reading `getResolvedPath()`; the loop guard
throws `FileSymlinkLoopException(PathFragment)` (the 8.x constructor); the
symlink target of an unresolved symlink artifact is read from
`UnresolvedSymlinkArtifactValue#getSymlinkTarget` (8.x has no
`FileArtifactValue#getUnresolvedSymlinkTarget`); and the 8.x
`CacheNotFoundException` rewinding `onErrorResumeNext` is preserved.

On the repo side, 8.x has no `RepositoryUtils` or merged
`RepositoryFetchFunction`, so the `ReplantSymlinksResult` record and the
main-repo-symlink exclusion are applied to
`RepositoryFunction.replantSymlinks` and the `RepositoryDelegatorFunction`
caching path (where remote caching is now gated on `safeForRemoteCache()`
and local caching on `safeForLocalCache()`). `VendorManager` keeps its
existing private `replantSymlinks`, so the upstream call-signature change
there does not apply.

Depends on the replant prerequisite (bazelbuild#28582) backported in the preceding
slot.

Fixes bazelbuild#28575.
Fixes bazelbuild#29656.
Fixes bazelbuild#29515.

Closes bazelbuild#28683.

PiperOrigin-RevId: 893359962
Change-Id: If0eb64e41858a4b9298c2609d83160b0ecbfd451
(cherry picked from commit 90eb56e)

Closes bazelbuild#29767.

PiperOrigin-RevId: 936532134
Change-Id: I48b63f6fd2d5f57e139175e3ed578d65f6abbd08
(cherry picked from commit c310e3e)
fmeum added a commit to fmeum/bazel that referenced this pull request Jul 14, 2026
…ing (bazelbuild#30035)

When using the remote repo contents cache, repo materialization now uses
the same code path as local action input prefetching for symlinks. This
ensures that each symlink is only materialized once and that there are
no races in case symlinking is not atomic (on Windows with default
settings). Along the way, remove unreachable code from `UploadManifest`.

Furthermore, when a remotely cached repo is partially materialized since
it contains action inputs, Skyframe provides the metadata of these
inputs after following all symlinks as it does for all source files.
Since (chains of) symlinks need to be preserved and materialized in the
same way as if the repo rule ran locally or was fully materialized as a
dependency of another repo rule, `AbstractActionInputPrefetcher` is
changed to walk the full chain of symlinks behind a prefetched external
repo path and replant every link verbatim on the host file system.

This proves very difficult to get right for repos with symlinks pointing
into the main repo as such symlinks would cross over into the host
filesystem. For this reason such symlinks now exclude a repo from being
cached remotely. Note that the local repo contents cache already doesn't
cache such repos. Since main repo files are readily available, this is
considered a minor loss in usefulness only.

This PR combines the backports of bazelbuild#28683 and bazelbuild#29767 into a single
commit, subsuming bazelbuild#30028: the symlink-materialization tests added
upstream in bazelbuild#28683 only pass on 8.x once the chain-walk change from
bazelbuild#29767 is in place, since repo files are materialized lazily through
`RemoteExternalOverlayFileSystem` rather than fully on disk as on
master.

8.x adaptation: `AbstractActionInputPrefetcher` on 8.x resolves input
paths via the Bazel 8.x-specific `resolveExecPath(execPath)` helper
(which avoids evaluating the WORKSPACE-named exec root when possible)
and exposes the exec root through the `execRoot()` method rather than an
`execRoot` field, so the new symlink handling is expressed in those
terms. Because `execRoot()` is not available during external repository
materialization, external repo paths are detected via a new
`isUnderExternalRepoRoot` helper (mirroring
`DirectoryTracker.setWritable`) instead of
`!inputPath.startsWith(execRoot())`; both `getSymlinks` and
`plantSymlink` use it. The chain-walking is re-expressed on the
exec-path-based symlink model (`resolveExecPath`, `execRoot()`,
`FileArtifactValue#getMaterializationExecPath`) rather than the master
model (`getResolvedPath`, `execRoot` field): the `Symlink` record
becomes `(Path linkPath, PathFragment targetPath)` as upstream, but the
materialization case resolves `getMaterializationExecPath()` through
`resolveExecPath` instead of reading `getResolvedPath()`; the loop guard
throws `FileSymlinkLoopException(PathFragment)` (the 8.x constructor);
the symlink target of an unresolved symlink artifact is read from
`UnresolvedSymlinkArtifactValue#getSymlinkTarget` (8.x has no
`FileArtifactValue#getUnresolvedSymlinkTarget`); and the 8.x
`CacheNotFoundException` rewinding `onErrorResumeNext` is preserved.

On the repo side, 8.x has no `RepositoryUtils` or merged
`RepositoryFetchFunction`, so the `ReplantSymlinksResult` record and the
main-repo-symlink exclusion are applied to
`RepositoryFunction.replantSymlinks` and the
`RepositoryDelegatorFunction` caching path (where remote caching is now
gated on `safeForRemoteCache()` and local caching on
`safeForLocalCache()`). `VendorManager` keeps its existing private
`replantSymlinks`, so the upstream call-signature change there does not
apply.

Depends on the replant prerequisite (bazelbuild#28582) backported in bazelbuild#30034.

Fixes bazelbuild#28575.
Fixes bazelbuild#29656.
Fixes bazelbuild#29515.

Closes bazelbuild#28683.

PiperOrigin-RevId: 893359962
Change-Id: If0eb64e41858a4b9298c2609d83160b0ecbfd451
(cherry picked from commit 90eb56e)

Closes bazelbuild#29767.

PiperOrigin-RevId: 936532134
Change-Id: I48b63f6fd2d5f57e139175e3ed578d65f6abbd08
(cherry picked from commit c310e3e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants