Skip to content

Commit

Permalink
Revert 279a87c
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed May 3, 2024
1 parent 15a856c commit bac9c36
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,7 @@ public static BazelLockFileValue getLockfileValue(
Matcher matcher = LOCKFILE_VERSION_PATTERN.matcher(json);
int version = matcher.find() ? Integer.parseInt(matcher.group(1)) : -1;
if (version == BazelLockFileValue.LOCK_FILE_VERSION) {
return GsonTypeAdapterUtil.createLockFileGson(
lockfilePath
.asPath()
.getParentDirectory()
.getRelative(LabelConstants.MODULE_DOT_BAZEL_FILE_NAME))
.fromJson(json, BazelLockFileValue.class);
return GsonTypeAdapterUtil.LOCKFILE_GSON.fromJson(json, BazelLockFileValue.class);
} else {
// This is an old version, its information can't be used.
if (lockfileMode == LockfileMode.ERROR) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,7 @@ private static void updateLockfile(Path workspaceRoot, BazelLockFileValue update
FileSystemUtils.writeContent(
lockfilePath.asPath(),
UTF_8,
GsonTypeAdapterUtil.createLockFileGson(
lockfilePath
.asPath()
.getParentDirectory()
.getRelative(LabelConstants.MODULE_DOT_BAZEL_FILE_NAME))
.toJson(updatedLockfile)
+ "\n");
GsonTypeAdapterUtil.LOCKFILE_GSON.toJson(updatedLockfile) + "\n");
} catch (IOException e) {
logger.atSevere().withCause(e).log(
"Error while updating MODULE.bazel.lock file: %s", e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static com.google.devtools.build.lib.bazel.bzlmod.DelegateTypeAdapterFactory.IMMUTABLE_MAP;
import static com.google.devtools.build.lib.bazel.bzlmod.DelegateTypeAdapterFactory.IMMUTABLE_SET;

import com.google.auto.value.AutoValue;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableTable;
Expand All @@ -32,7 +31,6 @@
import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
import com.google.devtools.build.lib.cmdline.RepositoryName;
import com.google.devtools.build.lib.rules.repository.RepoRecordedInput;
import com.google.devtools.build.lib.vfs.Path;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
Expand All @@ -49,7 +47,6 @@
import java.util.Base64;
import java.util.Optional;
import javax.annotation.Nullable;
import net.starlark.java.syntax.Location;

/**
* Utility class to hold type adapters and helper methods to get gson registered with type adapters
Expand Down Expand Up @@ -366,82 +363,6 @@ public ImmutableTable<Object, Object, Object> read(JsonReader jsonReader)
}
};

/**
* A variant of {@link Location} that converts the absolute path to the root module file to a
* constant and back.
*/
// protected only for @AutoValue
@GenerateTypeAdapter
@AutoValue
protected abstract static class RootModuleFileEscapingLocation {
// This marker string is neither a valid absolute path nor a valid URL and thus cannot conflict
// with any real module file location.
private static final String ROOT_MODULE_FILE_LABEL = "@@//:MODULE.bazel";

public abstract String file();

public abstract int line();

public abstract int column();

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

public static RootModuleFileEscapingLocation fromLocation(
Location location, String moduleFilePath) {
String file;
if (location.file().equals(moduleFilePath)) {
file = ROOT_MODULE_FILE_LABEL;
} else {
file = location.file();
}
return new AutoValue_GsonTypeAdapterUtil_RootModuleFileEscapingLocation(
file, location.line(), location.column());
}
}

private static final class LocationTypeAdapterFactory implements TypeAdapterFactory {

private final String moduleFilePath;

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

@Nullable
@Override
@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
if (typeToken.getRawType() != Location.class) {
return null;
}
TypeAdapter<RootModuleFileEscapingLocation> relativizedLocationTypeAdapter =
gson.getAdapter(RootModuleFileEscapingLocation.class);
return (TypeAdapter<T>)
new TypeAdapter<Location>() {

@Override
public void write(JsonWriter jsonWriter, Location location) throws IOException {
relativizedLocationTypeAdapter.write(
jsonWriter,
RootModuleFileEscapingLocation.fromLocation(location, moduleFilePath));
}

@Override
public Location read(JsonReader jsonReader) throws IOException {
return relativizedLocationTypeAdapter.read(jsonReader).toLocation(moduleFilePath);
}
};
}
}

private static final TypeAdapter<RepoRecordedInput.File> REPO_RECORDED_INPUT_FILE_TYPE_ADAPTER =
new TypeAdapter<>() {
@Override
Expand Down Expand Up @@ -522,17 +443,13 @@ public Optional<Checksum> read(JsonReader jsonReader) throws IOException {
}
}

public static Gson createLockFileGson(Path moduleFilePath) {
return newGsonBuilder()
.setPrettyPrinting()
.registerTypeAdapterFactory(new LocationTypeAdapterFactory(moduleFilePath))
.registerTypeAdapterFactory(new OptionalChecksumTypeAdapterFactory())
.create();
}
public static final Gson LOCKFILE_GSON =
newGsonBuilder()
.setPrettyPrinting()
.registerTypeAdapterFactory(new OptionalChecksumTypeAdapterFactory())
.create();

public static Gson createSingleExtensionUsagesValueHashGson() {
return newGsonBuilder().create();
}
public static final Gson SINGLE_EXTENSION_USAGES_VALUE_GSON = newGsonBuilder().create();

private static GsonBuilder newGsonBuilder() {
return new GsonBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,7 @@ public SkyValue compute(SkyKey skyKey, Environment env)
.setBzlTransitiveDigest(extension.getBzlTransitiveDigest())
.setUsagesDigest(
SingleExtensionUsagesValue.hashForEvaluation(
GsonTypeAdapterUtil.createSingleExtensionUsagesValueHashGson(),
usagesValue))
GsonTypeAdapterUtil.SINGLE_EXTENSION_USAGES_VALUE_GSON, usagesValue))
.setRecordedFileInputs(moduleExtensionResult.getRecordedFileInputs())
.setRecordedDirentsInputs(moduleExtensionResult.getRecordedDirentsInputs())
.setEnvVariables(extension.getEnvVars())
Expand Down Expand Up @@ -282,7 +281,7 @@ private SingleExtensionValue tryGettingValueFromLockFile(
// relevant for the evaluation of the extension.
if (!Arrays.equals(
SingleExtensionUsagesValue.hashForEvaluation(
GsonTypeAdapterUtil.createSingleExtensionUsagesValueHashGson(), usagesValue),
GsonTypeAdapterUtil.SINGLE_EXTENSION_USAGES_VALUE_GSON, usagesValue),
lockedExtension.getUsagesDigest())) {
diffRecorder.record("The usages of the extension '" + extensionId + "' have changed");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public RepoSpec getRepoSpec(ModuleKey key, ExtendedEventHandler eventHandler) {
"%s/modules/%s/%s/source.json"
.formatted(url, key.getName(), key.getVersion().toString()),
Optional.of(
GsonTypeAdapterUtil.createSingleExtensionUsagesValueHashGson()
GsonTypeAdapterUtil.SINGLE_EXTENSION_USAGES_VALUE_GSON
.toJson(repoSpec)
.getBytes(UTF_8))));
return repoSpec;
Expand Down

0 comments on commit bac9c36

Please sign in to comment.