Skip to content

Commit

Permalink
[7.3.1] Fix expected lockfile version to change based on `--incompati…
Browse files Browse the repository at this point in the history
…ble_use_plus_in_repo_names` (#23281)

We write a different lockfile version based on
`--incompatible_use_plus_in_repo_names` (see HACK in
`BazelLockFileModule.java`). But we don't _expect_ a different lockfile
version based on that flag; this causes us to consider the lockfile
_always_ out-of-date when that flag is set.

Fixes #23279.
  • Loading branch information
Wyverald authored Aug 13, 2024
1 parent e516460 commit d89b97d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
import com.google.devtools.build.lib.actions.FileValue;
import com.google.devtools.build.lib.bazel.repository.RepositoryOptions.LockfileMode;
import com.google.devtools.build.lib.cmdline.LabelConstants;
import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions;
import com.google.devtools.build.lib.profiler.Profiler;
import com.google.devtools.build.lib.profiler.ProfilerTask;
import com.google.devtools.build.lib.profiler.SilentCloseable;
import com.google.devtools.build.lib.server.FailureDetails.ExternalDeps.Code;
import com.google.devtools.build.lib.skyframe.PrecomputedValue;
import com.google.devtools.build.lib.skyframe.PrecomputedValue.Precomputed;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
Expand All @@ -40,6 +42,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import net.starlark.java.eval.StarlarkSemantics;

/** Reads the contents of the lock file into its value. */
public class BazelLockFileFunction implements SkyFunction {
Expand All @@ -66,14 +69,18 @@ public SkyValue compute(SkyKey skyKey, Environment env)
throws BazelLockfileFunctionException, InterruptedException {
RootedPath lockfilePath =
RootedPath.toRootedPath(Root.fromPath(rootDirectory), LabelConstants.MODULE_LOCKFILE_NAME);
StarlarkSemantics starlarkSemantics = PrecomputedValue.STARLARK_SEMANTICS.get(env);
if (starlarkSemantics == null) {
return null;
}

// Add dependency on the lockfile to recognize changes to it
if (env.getValue(FileValue.key(lockfilePath)) == null) {
return null;
}

try (SilentCloseable c = Profiler.instance().profile(ProfilerTask.BZLMOD, "parse lockfile")) {
return getLockfileValue(lockfilePath, LOCKFILE_MODE.get(env));
return getLockfileValue(lockfilePath, LOCKFILE_MODE.get(env), starlarkSemantics);
} catch (IOException
| JsonSyntaxException
| NullPointerException
Expand All @@ -97,13 +104,20 @@ public SkyValue compute(SkyKey skyKey, Environment env)
}

public static BazelLockFileValue getLockfileValue(
RootedPath lockfilePath, LockfileMode lockfileMode)
RootedPath lockfilePath, LockfileMode lockfileMode, StarlarkSemantics starlarkSemantics)
throws IOException, BazelLockfileFunctionException {
try {
String json = FileSystemUtils.readContent(lockfilePath.asPath(), UTF_8);
Matcher matcher = LOCKFILE_VERSION_PATTERN.matcher(json);
int version = matcher.find() ? Integer.parseInt(matcher.group(1)) : -1;
if (version == BazelLockFileValue.LOCK_FILE_VERSION) {
// HACK: We need to switch the expected lockfile version based on the value of
// `--incompatible_use_plus_in_repo_names`. See full explanation at
// BazelLockFileModule.java:120
int expectedVersion =
starlarkSemantics.getBool(BuildLanguageOptions.INCOMPATIBLE_USE_PLUS_IN_REPO_NAMES)
? BazelLockFileValue.LOCK_FILE_VERSION + 1
: BazelLockFileValue.LOCK_FILE_VERSION;
if (version == expectedVersion) {
return GsonTypeAdapterUtil.LOCKFILE_GSON.fromJson(json, BazelLockFileValue.class);
} else {
// This is an old version, its information can't be used.
Expand Down
6 changes: 6 additions & 0 deletions src/test/py/bazel/bzlmod/bazel_lockfile_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2597,6 +2597,12 @@ def testModuleExtensionRerunsOnGetenvChanges(self):
stderr = '\n'.join(stderr)
self.assertIn('LAZYEVAL_KEY=None', stderr)

def testLockFileVersionIsCorrectWithUsePlusFlag(self):
self.RunBazel(['mod', 'graph', '--incompatible_use_plus_in_repo_names'])
self.RunBazel(['clean', '--expunge'])
self.RunBazel(['mod', 'graph', '--incompatible_use_plus_in_repo_names',
'--lockfile_mode=error'])


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

0 comments on commit d89b97d

Please sign in to comment.