Skip to content

Commit

Permalink
Revert "Remove user specific path from the lockfile (Fixes bazelbuild…
Browse files Browse the repository at this point in the history
…#19621)"

This reverts commit 173bd2b.
  • Loading branch information
fmeum committed Apr 26, 2024
1 parent 4df3276 commit 387fb14
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.actions.FileValue;
import com.google.devtools.build.lib.bazel.repository.RepositoryOptions.LockfileMode;
Expand Down Expand Up @@ -78,7 +77,7 @@ public SkyValue compute(SkyKey skyKey, Environment env)

BazelLockFileValue lockfileValue;
try (SilentCloseable c = Profiler.instance().profile(ProfilerTask.BZLMOD, "parse lockfile")) {
lockfileValue = getLockfileValue(lockfilePath, rootDirectory);
lockfileValue = getLockfileValue(lockfilePath);
} catch (IOException | JsonSyntaxException | NullPointerException e) {
throw new BazelLockfileFunctionException(
ExternalDepsException.withMessage(
Expand All @@ -101,8 +100,7 @@ public SkyValue compute(SkyKey skyKey, Environment env)
return lockfileValue;
}

public static BazelLockFileValue getLockfileValue(RootedPath lockfilePath, Path rootDirectory)
throws IOException {
public static BazelLockFileValue getLockfileValue(RootedPath lockfilePath) throws IOException {
BazelLockFileValue bazelLockFileValue;
try {
String json = FileSystemUtils.readContent(lockfilePath.asPath(), UTF_8);
Expand All @@ -114,8 +112,7 @@ public static BazelLockFileValue getLockfileValue(RootedPath lockfilePath, Path
lockfilePath
.asPath()
.getParentDirectory()
.getRelative(LabelConstants.MODULE_DOT_BAZEL_FILE_NAME),
rootDirectory)
.getRelative(LabelConstants.MODULE_DOT_BAZEL_FILE_NAME))
.fromJson(json, BazelLockFileValue.class);
} else {
// This is an old version, needs to be updated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,7 @@ private static void updateLockfile(Path workspaceRoot, BazelLockFileValue update
lockfilePath
.asPath()
.getParentDirectory()
.getRelative(LabelConstants.MODULE_DOT_BAZEL_FILE_NAME),
workspaceRoot)
.getRelative(LabelConstants.MODULE_DOT_BAZEL_FILE_NAME))
.toJson(updatedLockfile)
+ "\n");
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,23 +384,23 @@ protected abstract static class RootModuleFileEscapingLocation {

public abstract int column();

public Location toLocation(String moduleFilePath, String workspaceRoot) {
public Location toLocation(String moduleFilePath) {
String file;
if (file().equals(ROOT_MODULE_FILE_LABEL)) {
file = moduleFilePath;
} else {
file = file().replace("%workspace%", workspaceRoot);
file = file();
}
return Location.fromFileLineColumn(file, line(), column());
}

public static RootModuleFileEscapingLocation fromLocation(
Location location, String moduleFilePath, String workspaceRoot) {
Location location, String moduleFilePath) {
String file;
if (location.file().equals(moduleFilePath)) {
file = ROOT_MODULE_FILE_LABEL;
} else {
file = location.file().replace(workspaceRoot, "%workspace%");
file = location.file();
}
return new AutoValue_GsonTypeAdapterUtil_RootModuleFileEscapingLocation(
file, location.line(), location.column());
Expand All @@ -410,11 +410,9 @@ public static RootModuleFileEscapingLocation fromLocation(
private static final class LocationTypeAdapterFactory implements TypeAdapterFactory {

private final String moduleFilePath;
private final String workspaceRoot;

public LocationTypeAdapterFactory(Path moduleFilePath, Path workspaceRoot) {
public LocationTypeAdapterFactory(Path moduleFilePath) {
this.moduleFilePath = moduleFilePath.getPathString();
this.workspaceRoot = workspaceRoot.getPathString();
}

@Nullable
Expand All @@ -433,15 +431,12 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
public void write(JsonWriter jsonWriter, Location location) throws IOException {
relativizedLocationTypeAdapter.write(
jsonWriter,
RootModuleFileEscapingLocation.fromLocation(
location, moduleFilePath, workspaceRoot));
RootModuleFileEscapingLocation.fromLocation(location, moduleFilePath));
}

@Override
public Location read(JsonReader jsonReader) throws IOException {
return relativizedLocationTypeAdapter
.read(jsonReader)
.toLocation(moduleFilePath, workspaceRoot);
return relativizedLocationTypeAdapter.read(jsonReader).toLocation(moduleFilePath);
}
};
}
Expand Down Expand Up @@ -527,10 +522,10 @@ public Optional<Checksum> read(JsonReader jsonReader) throws IOException {
}
}

public static Gson createLockFileGson(Path moduleFilePath, Path workspaceRoot) {
public static Gson createLockFileGson(Path moduleFilePath) {
return newGsonBuilder()
.setPrettyPrinting()
.registerTypeAdapterFactory(new LocationTypeAdapterFactory(moduleFilePath, workspaceRoot))
.registerTypeAdapterFactory(new LocationTypeAdapterFactory(moduleFilePath))
.registerTypeAdapterFactory(new OptionalChecksumTypeAdapterFactory())
.create();
}
Expand Down
14 changes: 3 additions & 11 deletions src/test/py/bazel/bzlmod/bazel_lockfile_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1561,17 +1561,9 @@ def testLockfileWithNoUserSpecificPath(self):
['build', '--registry=file:///%workspace%/registry', '//:lala']
)

with open('MODULE.bazel.lock', 'r') as json_file:
lockfile = json.load(json_file)
ss_dep = lockfile['moduleDepGraph']['[email protected]']
remote_patches = ss_dep['repoSpec']['attributes']['remote_patches']
ext_usage_location = ss_dep['extensionUsages'][0]['location']['file']

self.assertNotIn(self.my_registry.getURL(), ext_usage_location)
self.assertIn('%workspace%', ext_usage_location)
for key in remote_patches.keys():
self.assertNotIn(self.my_registry.getURL(), key)
self.assertIn('%workspace%', key)
with open('MODULE.bazel.lock', 'r') as f:
self.assertNotIn(self.my_registry.getURL(), f.read())

finally:
self.my_registry.stop()

Expand Down

0 comments on commit 387fb14

Please sign in to comment.