Skip to content

Commit

Permalink
Use lockfile version to update or error
Browse files Browse the repository at this point in the history
If version is old, update without parsing, or error with requesting user to update

PiperOrigin-RevId: 554426312
Change-Id: Ib59b2009595feaf7c9a9e207929df6e794af0b77
  • Loading branch information
SalmaSamy authored and Copybara-Service committed Aug 7, 2023
1 parent ead0e42 commit 15b1575
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public SkyValue compute(SkyKey skyKey, Environment env)
if (!lockfileMode.equals(LockfileMode.OFF)) {
BazelLockFileValue updateLockfile =
lockfile.toBuilder()
.setLockFileVersion(BazelLockFileValue.LOCK_FILE_VERSION)
.setModuleFileHash(root.getModuleFileHash())
.setFlags(flags)
.setLocalOverrideHashes(localOverrideHashes)
Expand Down Expand Up @@ -183,7 +184,7 @@ static BzlmodFlagsAndEnvVars getFlagsAndEnvVars(Environment env) throws Interrup
ImmutableMap<String, String> moduleOverrides =
ModuleFileFunction.MODULE_OVERRIDES.get(env).entrySet().stream()
.collect(
toImmutableMap(e -> e.getKey(), e -> ((LocalPathOverride) e.getValue()).getPath()));
toImmutableMap(Entry::getKey, e -> ((LocalPathOverride) e.getValue()).getPath()));

ImmutableList<String> yankedVersions =
ImmutableList.copyOf(YankedVersionsUtil.ALLOWED_YANKED_VERSIONS.get(env));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,18 @@
import com.google.gson.JsonSyntaxException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nullable;

/** Reads the contents of the lock file into its value. */
public class BazelLockFileFunction implements SkyFunction {

public static final Precomputed<LockfileMode> LOCKFILE_MODE = new Precomputed<>("lockfile_mode");

private static final Pattern LOCKFILE_VERSION_PATTERN =
Pattern.compile("\"lockFileVersion\":\\s*(\\d+)");

private final Path rootDirectory;

private static final BzlmodFlagsAndEnvVars EMPTY_FLAGS =
Expand Down Expand Up @@ -92,13 +97,21 @@ public static BazelLockFileValue getLockfileValue(RootedPath lockfilePath) throw
BazelLockFileValue bazelLockFileValue;
try {
String json = FileSystemUtils.readContent(lockfilePath.asPath(), UTF_8);
bazelLockFileValue =
GsonTypeAdapterUtil.createLockFileGson(
lockfilePath
.asPath()
.getParentDirectory()
.getRelative(LabelConstants.MODULE_DOT_BAZEL_FILE_NAME))
.fromJson(json, BazelLockFileValue.class);
Matcher matcher = LOCKFILE_VERSION_PATTERN.matcher(json);
int version = matcher.find() ? Integer.parseInt(matcher.group(1)) : -1;
if (version == BazelLockFileValue.LOCK_FILE_VERSION) {
bazelLockFileValue =
GsonTypeAdapterUtil.createLockFileGson(
lockfilePath
.asPath()
.getParentDirectory()
.getRelative(LabelConstants.MODULE_DOT_BAZEL_FILE_NAME))
.fromJson(json, BazelLockFileValue.class);
} else {
// This is an old version, needs to be updated
// Keep old version to recognize the problem in error mode
bazelLockFileValue = EMPTY_LOCKFILE.toBuilder().setLockFileVersion(version).build();
}
} catch (FileNotFoundException e) {
bazelLockFileValue = EMPTY_LOCKFILE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ public ImmutableList<String> getModuleAndFlagsDiff(
ImmutableMap<String, String> localOverrideHashes,
BzlmodFlagsAndEnvVars flags) {
ImmutableList.Builder<String> moduleDiff = new ImmutableList.Builder<>();
if (getLockFileVersion() != BazelLockFileValue.LOCK_FILE_VERSION) {
return moduleDiff
.add(
"the version of the lockfile is not compatible with the current Bazel, please run"
+ " with '--lockfile_mode=update'")
.build();
}
if (!moduleFileHash.equals(getModuleFileHash())) {
moduleDiff.add("the root MODULE.bazel has been modified");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,9 @@ public void invalidLockfileEmptyFile() throws Exception {
fail(result.getError().toString());
}

scratch.overwriteFile(rootDirectory.getRelative("MODULE.bazel.lock").getPathString(), "{}");
scratch.overwriteFile(
rootDirectory.getRelative("MODULE.bazel.lock").getPathString(),
"{\"lockFileVersion\": " + BazelLockFileValue.LOCK_FILE_VERSION + "}");

result = evaluator.evaluate(ImmutableList.of(BazelLockFileValue.KEY), evaluationContext);
if (!result.hasError()) {
Expand Down
34 changes: 34 additions & 0 deletions src/test/py/bazel/bzlmod/bazel_lockfile_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,40 @@ def testModuleExtensionWithFile(self):
_, _, stderr = self.RunBazel(['build', '@hello//:all'])
self.assertIn('I have changed now!', ''.join(stderr))

def testOldVersion(self):
self.ScratchFile('MODULE.bazel')
self.ScratchFile('BUILD', ['filegroup(name = "hello")'])
self.RunBazel(['build', '--nobuild', '//:all'])

# Set version to old
with open('MODULE.bazel.lock', 'r') as json_file:
data = json.load(json_file)
data['lockFileVersion'] = 0
with open('MODULE.bazel.lock', 'w') as json_file:
json.dump(data, json_file, indent=4)

# Run in error mode
exit_code, _, stderr = self.RunBazel(
['build', '--nobuild', '--lockfile_mode=error', '//:all'],
allow_failure=True,
)
self.AssertExitCode(exit_code, 48, stderr)
self.assertIn(
(
'ERROR: Error computing the main repository mapping: Lock file is'
' no longer up-to-date because: the version of the lockfile is not'
' compatible with the current Bazel, please run with'
" '--lockfile_mode=update'"
),
stderr,
)

# Run again with update
self.RunBazel(['build', '--nobuild', '//:all'])
with open('MODULE.bazel.lock', 'r') as json_file:
data = json.load(json_file)
self.assertEqual(data['lockFileVersion'], 1)


if __name__ == '__main__':
unittest.main()

0 comments on commit 15b1575

Please sign in to comment.