diff --git a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelLockFileFunction.java b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelLockFileFunction.java index 5e34b4c3e83481..4c8aa872646c60 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelLockFileFunction.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelLockFileFunction.java @@ -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; @@ -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( @@ -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); @@ -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 diff --git a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelLockFileModule.java b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelLockFileModule.java index 9d6cd27b326c96..481e5387395e6c 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelLockFileModule.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelLockFileModule.java @@ -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) { diff --git a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/GsonTypeAdapterUtil.java b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/GsonTypeAdapterUtil.java index 64e07c8f9428f1..3d346798852bb3 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/GsonTypeAdapterUtil.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/GsonTypeAdapterUtil.java @@ -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()); @@ -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 @@ -433,15 +431,12 @@ public TypeAdapter create(Gson gson, TypeToken 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); } }; } @@ -527,10 +522,10 @@ public Optional 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(); } diff --git a/src/test/py/bazel/bzlmod/bazel_lockfile_test.py b/src/test/py/bazel/bzlmod/bazel_lockfile_test.py index a2457b9d544a37..1a739b220dc2b8 100644 --- a/src/test/py/bazel/bzlmod/bazel_lockfile_test.py +++ b/src/test/py/bazel/bzlmod/bazel_lockfile_test.py @@ -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']['ss@1.3-1'] - 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()