Skip to content
This repository was archived by the owner on Nov 10, 2023. It is now read-only.

Commit 606f6d5

Browse files
KapJIfacebook-github-bot
authored andcommitted
Back out "Reduce Rust compile rule path length"
Summary: Original commit changeset: 8ef9cf0fdeef Reverts D31888529. It breaks some tests (T107229114) and makes compilation error messages less helpful: https://fb.workplace.com/groups/rust.language/posts/7389150347800183/ Rust 1.58 has support for long paths on Windows which should solve original issue. rust-lang/rust#89174 It's currently in beta, stable release will happen on Jan 13: https://forge.rust-lang.org/ But I think we can use beta version for some time on Windows. Reviewed By: jsgf fbshipit-source-id: 9af1a5631ab56b8bb285da7cc3044eb8ba1bde85
1 parent 5c67256 commit 606f6d5

File tree

4 files changed

+65
-69
lines changed

4 files changed

+65
-69
lines changed

src/com/facebook/buck/features/rust/RustCompileRule.java

+18-8
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
import com.google.common.collect.ImmutableSortedMap;
5555
import com.google.common.collect.Maps;
5656
import java.nio.file.Path;
57-
import java.nio.file.Paths;
5857
import java.util.Optional;
5958

6059
/** Generate a rustc command line with all appropriate dependencies in place. */
@@ -91,12 +90,11 @@ protected RustCompileRule(
9190
ImmutableList<Arg> depArgs,
9291
ImmutableList<Arg> linkerArgs,
9392
ImmutableSortedMap<String, Arg> environment,
94-
ImmutableSortedMap<SourcePath, String> mappedSources,
93+
ImmutableSortedMap<SourcePath, Optional<String>> mappedSources,
9594
String rootModule,
9695
RemapSrcPaths remapSrcPaths,
9796
Optional<String> xcrunSdkPath,
9897
boolean withDownwardApi) {
99-
10098
super(
10199
buildTarget,
102100
projectFilesystem,
@@ -130,7 +128,7 @@ public static RustCompileRule from(
130128
ImmutableList<Arg> depArgs,
131129
ImmutableList<Arg> linkerArgs,
132130
ImmutableSortedMap<String, Arg> environment,
133-
ImmutableSortedMap<SourcePath, String> mappedSources,
131+
ImmutableSortedMap<SourcePath, Optional<String>> mappedSources,
134132
String rootModule,
135133
RemapSrcPaths remapSrcPaths,
136134
Optional<String> xcrunSdkPath,
@@ -186,7 +184,7 @@ static class Impl implements Buildable {
186184
@AddToRuleKey private final String rootModule;
187185
@AddToRuleKey private final OutputPath output;
188186

189-
@AddToRuleKey private final ImmutableSortedMap<SourcePath, String> mappedSources;
187+
@AddToRuleKey private final ImmutableSortedMap<SourcePath, Optional<String>> mappedSources;
190188

191189
@AddToRuleKey private final RustBuckConfig.RemapSrcPaths remapSrcPaths;
192190

@@ -208,7 +206,7 @@ public Impl(
208206
ImmutableSortedMap<String, Arg> environment,
209207
String rootModule,
210208
String outputName,
211-
ImmutableSortedMap<SourcePath, String> mappedSources,
209+
ImmutableSortedMap<SourcePath, Optional<String>> mappedSources,
212210
RemapSrcPaths remapSrcPaths,
213211
Optional<String> xcrunpath,
214212
boolean withDownwardApi) {
@@ -255,7 +253,20 @@ public ImmutableList<Step> getBuildSteps(
255253
mappedSources.entrySet().stream()
256254
.collect(
257255
ImmutableMap.toImmutableMap(
258-
ent -> Paths.get(ent.getValue()),
256+
ent -> {
257+
Path path;
258+
if (ent.getValue().isPresent()) {
259+
path =
260+
buildTarget
261+
.getCellRelativeBasePath()
262+
.getPath()
263+
.toPath(filesystem.getFileSystem())
264+
.resolve(ent.getValue().get());
265+
} else {
266+
path = resolver.getCellUnsafeRelPath(ent.getKey()).getPath();
267+
}
268+
return path;
269+
},
259270
ent -> resolver.getAbsolutePath(ent.getKey()).getPath()))));
260271
steps.addAll(
261272
CxxPrepareForLinkStep.create(
@@ -296,7 +307,6 @@ protected ImmutableList<String> getShellCommandInternal(
296307
ImmutableList<String> linkerCmd = linker.getCommandPrefix(resolver);
297308
ImmutableList.Builder<String> cmd = ImmutableList.builder();
298309
Path src = scratchDir.resolve(rootModule);
299-
300310
cmd.addAll(compiler.getCommandPrefix(resolver));
301311
if (executionContext.getAnsi().isAnsiTerminal()) {
302312
cmd.add("--color=always");

src/com/facebook/buck/features/rust/RustCompileUtils.java

+44-53
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@
8282
import com.google.common.hash.Hashing;
8383
import java.nio.charset.StandardCharsets;
8484
import java.nio.file.Path;
85-
import java.nio.file.Paths;
8685
import java.util.Collection;
8786
import java.util.List;
8887
import java.util.Map;
@@ -112,7 +111,7 @@ public static RustCompileRule requireBuild(
112111
CrateType crateType,
113112
Optional<String> edition,
114113
LinkableDepType depType,
115-
ImmutableSortedMap<SourcePath, String> mappedSources,
114+
ImmutableSortedMap<SourcePath, Optional<String>> mappedSources,
116115
String rootModule,
117116
boolean forceRlib,
118117
boolean preferStatic,
@@ -246,7 +245,7 @@ private static RustCompileRule createBuild(
246245
Optional<String> edition,
247246
LinkableDepType depType,
248247
boolean rpath,
249-
ImmutableSortedMap<SourcePath, String> mappedSources,
248+
ImmutableSortedMap<SourcePath, Optional<String>> mappedSources,
250249
String rootModule,
251250
boolean forceRlib,
252251
boolean preferStatic,
@@ -642,7 +641,7 @@ public static BinaryWrapperRule createBinaryBuildRule(
642641

643642
CxxPlatform cxxPlatform = rustPlatform.getCxxPlatform();
644643

645-
Pair<String, ImmutableSortedMap<SourcePath, String>> rootModuleAndSources =
644+
Pair<String, ImmutableSortedMap<SourcePath, Optional<String>>> rootModuleAndSources =
646645
getRootModuleAndSources(
647646
projectFilesystem,
648647
buildTarget,
@@ -830,15 +829,16 @@ public static Optional<String> getCrateRoot(
830829
SourcePathResolverAdapter resolver,
831830
String crate,
832831
ImmutableSet<String> defaults,
833-
Stream<String> sources) {
832+
Stream<Path> sources) {
834833
String crateName = String.format("%s.rs", crate);
835834
ImmutableList<String> res =
836835
sources
837836
.filter(
838837
src -> {
839-
String name = Paths.get(src).getFileName().toString();
838+
String name = src.getFileName().toString();
840839
return defaults.contains(name) || name.equals(crateName);
841840
})
841+
.map(src -> src.toString())
842842
.collect(ImmutableList.toImmutableList());
843843

844844
if (res.size() == 1) {
@@ -854,7 +854,7 @@ public static Optional<String> getCrateRoot(
854854
* module is what's passed to rustc as the input source file, and may not exist until the
855855
* symlinking phase happens.
856856
*/
857-
static Pair<String, ImmutableSortedMap<SourcePath, String>> getRootModuleAndSources(
857+
static Pair<String, ImmutableSortedMap<SourcePath, Optional<String>>> getRootModuleAndSources(
858858
ProjectFilesystem projectFilesystem,
859859
BuildTarget target,
860860
ActionGraphBuilder graphBuilder,
@@ -865,64 +865,55 @@ static Pair<String, ImmutableSortedMap<SourcePath, String>> getRootModuleAndSour
865865
ImmutableSortedSet<SourcePath> srcs,
866866
ImmutableSortedMap<SourcePath, String> mappedSrcs) {
867867

868-
ImmutableSortedMap.Builder<SourcePath, String> fixedBuilder = ImmutableSortedMap.naturalOrder();
869-
870-
Path buildTargetPath =
871-
target.getCellRelativeBasePath().getPath().toPath(projectFilesystem.getFileSystem());
872-
873-
SourcePathResolverAdapter resolver = graphBuilder.getSourcePathResolver();
868+
ImmutableSortedMap.Builder<SourcePath, Optional<String>> fixedBuilder =
869+
ImmutableSortedMap.naturalOrder();
874870

875871
srcs.stream()
876872
.map(
877873
src ->
878874
CxxGenruleDescription.fixupSourcePath(graphBuilder, cxxPlatform.getFlavor(), src))
879-
.forEach(
880-
src -> {
881-
SourcePath srcPath =
882-
CxxGenruleDescription.fixupSourcePath(graphBuilder, cxxPlatform.getFlavor(), src);
883-
Path candidatePath = resolver.getCellUnsafeRelPath(src).getPath();
884-
885-
// These are files with no user-defined virtual path, they are either
886-
// generated (ie with a genrule) or references files in the source
887-
// tree
888-
889-
// If the path references files in the source tree
890-
if (candidatePath.startsWith(buildTargetPath)) {
891-
// We relativize them to reduce path amplification - mostly for
892-
// windows's sake
893-
fixedBuilder.put(srcPath, buildTargetPath.relativize(candidatePath).toString());
894-
} else {
895-
896-
// These are generated files, since these are generated paths with hashes, they are
897-
// rewritable.
898-
fixedBuilder.put(
899-
srcPath, resolver.getAbsolutePath(srcPath).getPath().getFileName().toString());
900-
}
901-
});
875+
.forEach(src -> fixedBuilder.put(src, Optional.empty()));
902876
mappedSrcs
903877
.entrySet()
904878
.forEach(
905-
ent -> {
906-
SourcePath sourcePath =
907-
CxxGenruleDescription.fixupSourcePath(
908-
graphBuilder, cxxPlatform.getFlavor(), ent.getKey());
909-
Path virtualPath = Paths.get(ent.getValue()).normalize();
910-
// These are rs files with user-defined virtual path, usually these are
911-
// the from mapped_srcs
912-
// Ban remappings that contain paths that go outside the base path
913-
if (virtualPath.startsWith("..")) {
914-
throw new HumanReadableException(
915-
"Mapped sources must be forward paths ie. no ../ ");
916-
}
917-
fixedBuilder.put(sourcePath, virtualPath.toString());
918-
});
919-
920-
ImmutableSortedMap<SourcePath, String> fixed = fixedBuilder.build();
879+
ent ->
880+
fixedBuilder.put(
881+
CxxGenruleDescription.fixupSourcePath(
882+
graphBuilder, cxxPlatform.getFlavor(), ent.getKey()),
883+
Optional.of(ent.getValue())));
884+
885+
ImmutableSortedMap<SourcePath, Optional<String>> fixed = fixedBuilder.build();
886+
887+
SourcePathResolverAdapter resolver = graphBuilder.getSourcePathResolver();
888+
Stream<Path> filenames =
889+
Stream.concat(
890+
srcs.stream()
891+
.map(
892+
src ->
893+
CxxGenruleDescription.fixupSourcePath(
894+
graphBuilder, cxxPlatform.getFlavor(), src))
895+
.map(sp -> resolver.getCellUnsafeRelPath(sp).getPath()),
896+
mappedSrcs.values().stream()
897+
.map(
898+
path ->
899+
target
900+
.getCellRelativeBasePath()
901+
.getPath()
902+
.toPath(projectFilesystem.getFileSystem())
903+
.resolve(path)));
921904

922905
Optional<String> rootModule =
923906
crateRoot
907+
.map(
908+
name ->
909+
target
910+
.getCellRelativeBasePath()
911+
.getPath()
912+
.toPath(projectFilesystem.getFileSystem())
913+
.resolve(name)
914+
.toString())
924915
.map(Optional::of)
925-
.orElseGet(() -> getCrateRoot(resolver, crate, defaultRoots, fixed.values().stream()));
916+
.orElseGet(() -> getCrateRoot(resolver, crate, defaultRoots, filenames));
926917

927918
return new Pair<>(
928919
rootModule.orElseThrow(

src/com/facebook/buck/features/rust/RustLibraryDescription.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ private RustCompileRule requireLibraryBuild(
115115
RustLibraryDescriptionArg args,
116116
Iterable<BuildRule> deps,
117117
ImmutableMap<String, BuildTarget> depsAliases) {
118-
Pair<String, ImmutableSortedMap<SourcePath, String>> rootModuleAndSources =
118+
Pair<String, ImmutableSortedMap<SourcePath, Optional<String>>> rootModuleAndSources =
119119
RustCompileUtils.getRootModuleAndSources(
120120
projectFilesystem,
121121
buildTarget,

test/com/facebook/buck/features/rust/RustCompileTest.java

+2-7
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ private FakeRustCompileRule(
289289
srcs.stream()
290290
.collect(
291291
ImmutableSortedMap.toImmutableSortedMap(
292-
Comparator.naturalOrder(), src -> src, src -> "random")),
292+
Comparator.naturalOrder(), src -> src, src -> Optional.empty())),
293293
rootModule,
294294
RustBuckConfig.RemapSrcPaths.NO,
295295
Optional.empty(),
@@ -308,12 +308,7 @@ static FakeRustCompileRule from(String target, ImmutableSortedSet<SourcePath> sr
308308
ImmutableSet.of("main.rs", "lib.rs"),
309309
srcs.stream()
310310
.map(
311-
sp ->
312-
ruleFinder
313-
.getSourcePathResolver()
314-
.getCellUnsafeRelPath(sp)
315-
.getPath()
316-
.toString()));
311+
sp -> ruleFinder.getSourcePathResolver().getCellUnsafeRelPath(sp).getPath()));
317312

318313
if (!root.isPresent()) {
319314
throw new HumanReadableException("No crate root source identified");

0 commit comments

Comments
 (0)