forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not force unresolved symlinks to be absolute
The documentation of `ctx.actions.symlink(target_path = ...)` states that the given path is used as the symlink target without any changes, but in reality this path has so far always been converted into an absolute path by prepending the exec root. This is changed by this commit, which gets very close to the documented behavior by preserving the target path as is except for the standard normalization applied by `PathFragment`. Improving the situation even further would require modifying or adding to Bazel's core file system API, which may be done in a follow-up PR. Since relative symlinks only resolve correctly at the location they were originally created, the logic for staging symlinks as runfiles in the sandbox necessarily becomes more complicated: Analogously to the case of local unsandboxed execution, symlinks in runfiles are now staged as symlinks to the original exec path of the relative symlink under the sandboxed exec root as well as the relative symlink itself in that location. As a side-effect of the switch to relative symlinks, this commit resolves a non-hermeticity issue observed in bazelbuild#10298 (comment) Integration tests are added to verify that symlinks staged in the sandbox are no longer resolved non-hermetically, including the case where they are contained in runfiles. Fixes bazelbuild#14224
- Loading branch information
Showing
6 changed files
with
267 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 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 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
112 changes: 112 additions & 0 deletions
112
src/main/java/com/google/devtools/build/lib/analysis/starlark/UnresolvedSymlinkAction.java
This file contains 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,112 @@ | ||
// Copyright 2022 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.devtools.build.lib.analysis.starlark; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import com.google.devtools.build.lib.actions.AbstractAction; | ||
import com.google.devtools.build.lib.actions.ActionExecutionContext; | ||
import com.google.devtools.build.lib.actions.ActionExecutionException; | ||
import com.google.devtools.build.lib.actions.ActionKeyContext; | ||
import com.google.devtools.build.lib.actions.ActionOwner; | ||
import com.google.devtools.build.lib.actions.ActionResult; | ||
import com.google.devtools.build.lib.actions.Artifact; | ||
import com.google.devtools.build.lib.actions.Artifact.ArtifactExpander; | ||
import com.google.devtools.build.lib.analysis.actions.SymlinkAction; | ||
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; | ||
import com.google.devtools.build.lib.collect.nestedset.Order; | ||
import com.google.devtools.build.lib.server.FailureDetails; | ||
import com.google.devtools.build.lib.server.FailureDetails.FailureDetail; | ||
import com.google.devtools.build.lib.server.FailureDetails.SymlinkAction.Code; | ||
import com.google.devtools.build.lib.util.DetailedExitCode; | ||
import com.google.devtools.build.lib.util.Fingerprint; | ||
import com.google.devtools.build.lib.vfs.Path; | ||
import com.google.devtools.build.lib.vfs.PathFragment; | ||
import java.io.IOException; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Action to create a possibly unresolved symbolic link to a raw path. | ||
* | ||
* To create a symlink to a known-to-exist target with alias semantics similar to a true copy of the | ||
* input, use {@link SymlinkAction} instead. | ||
*/ | ||
public final class UnresolvedSymlinkAction extends AbstractAction { | ||
private static final String GUID = "0f302651-602c-404b-881c-58913193cfe7"; | ||
|
||
private final String target; | ||
private final String progressMessage; | ||
|
||
public UnresolvedSymlinkAction( | ||
ActionOwner owner, | ||
Artifact primaryOutput, | ||
String target, | ||
String progressMessage) { | ||
super( | ||
owner, | ||
NestedSetBuilder.emptySet(Order.STABLE_ORDER), | ||
ImmutableSet.of(primaryOutput)); | ||
this.target = target; | ||
this.progressMessage = progressMessage; | ||
} | ||
|
||
@Override | ||
public ActionResult execute(ActionExecutionContext actionExecutionContext) | ||
throws ActionExecutionException { | ||
|
||
Path outputPath = actionExecutionContext.getInputPath(getPrimaryOutput()); | ||
try { | ||
// TODO: PathFragment#create normalizes the symlink target, which may change how it resolves | ||
// when combined with directory symlinks. Ideally, Bazel's file system abstraction would | ||
// offer a way to create symlinks without any preprocessing of the target. | ||
outputPath.createSymbolicLink(PathFragment.create(target)); | ||
} catch (IOException e) { | ||
String message = | ||
String.format( | ||
"failed to create symbolic link '%s' to '%s' due to I/O error: %s", | ||
getPrimaryOutput().getExecPathString(), target, e.getMessage()); | ||
DetailedExitCode code = createDetailedExitCode(message, Code.LINK_CREATION_IO_EXCEPTION); | ||
throw new ActionExecutionException(message, e, this, false, code); | ||
} | ||
|
||
return ActionResult.EMPTY; | ||
} | ||
|
||
@Override | ||
protected void computeKey( | ||
ActionKeyContext actionKeyContext, | ||
@Nullable ArtifactExpander artifactExpander, | ||
Fingerprint fp) { | ||
fp.addString(GUID); | ||
fp.addString(target); | ||
} | ||
|
||
@Override | ||
public String getMnemonic() { | ||
return "UnresolvedSymlink"; | ||
} | ||
|
||
@Override | ||
protected String getRawProgressMessage() { | ||
return progressMessage; | ||
} | ||
|
||
private static DetailedExitCode createDetailedExitCode(String message, Code detailedCode) { | ||
return DetailedExitCode.of( | ||
FailureDetail.newBuilder() | ||
.setMessage(message) | ||
.setSymlinkAction(FailureDetails.SymlinkAction.newBuilder().setCode(detailedCode)) | ||
.build()); | ||
} | ||
} |
This file contains 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 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