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

Commit 55d8639

Browse files
Mateusz Kaczanowskifacebook-github-bot
authored andcommitted
fix: buck project was removing files generated within the same directory as part of a cleanup + add project_path option
Summary: The "cleanup" loop was removing files that were generated by the same loop (assuming there are more than one file in the directory). It's fixed within this diff. To the 'project_path': The vendor_path is a list of vendor paths so it's not really valid to use it as a destination for generated files. Reviewed By: styurin fbshipit-source-id: a5163c5a98
1 parent 4c5fc42 commit 55d8639

5 files changed

Lines changed: 104 additions & 18 deletions

File tree

docs/concept/buckconfig.soy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,6 +2145,17 @@ cxx_library(
21452145
{/param}
21462146
{/call}
21472147

2148+
{call buckconfig.entry}
2149+
{param section: 'go' /}
2150+
{param name: 'project_path' /}
2151+
{param example_value: 'third-party/go' /}
2152+
{param description}
2153+
You can specify the path where <code>buck project</code> will store dynamically
2154+
generated files (ex. genrule). This is extension to <code>$GOPATH</code>, particularly
2155+
usefuly while working with native go toolchain or IDE's.
2156+
{/param}
2157+
{/call}
2158+
21482159
{call buckconfig.section}
21492160
{param name: 'groovy' /}
21502161
{param description}

src/com/facebook/buck/features/go/GoBuckConfig.java

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,77 @@
2929
public class GoBuckConfig {
3030

3131
static final String SECTION = "go";
32-
3332
private final BuckConfig delegate;
3433

34+
private static final String VENDOR_PATH = "vendor_path";
35+
private static final String PROJECT_PATH = "project_path";
36+
private static final String TEST_MAIN_GEN = "test_main_gen";
37+
private static final String DEFAULT_PLATFORM = "default_platform";
38+
private static final String PREFIX = "prefix";
39+
3540
public GoBuckConfig(BuckConfig delegate) {
3641
this.delegate = delegate;
3742
}
3843

39-
Optional<String> getDefaultPlatform() {
40-
return delegate.getValue(SECTION, "default_platform");
41-
}
42-
4344
public BuckConfig getDelegate() {
4445
return delegate;
4546
}
4647

48+
/**
49+
* Get default Go platform
50+
*
51+
* @return Go platform
52+
*/
53+
Optional<String> getDefaultPlatform() {
54+
return delegate.getValue(SECTION, DEFAULT_PLATFORM);
55+
}
56+
57+
/**
58+
* Get package name either from build target attribute (package_name) if provided, otherwise use
59+
* BuildTarget base path.
60+
*
61+
* @return Package name
62+
*/
4763
Path getDefaultPackageName(BuildTarget target) {
48-
Path prefix = Paths.get(delegate.getValue(SECTION, "prefix").orElse(""));
64+
Path prefix = Paths.get(delegate.getValue(SECTION, PREFIX).orElse(""));
4965
return prefix.resolve(target.getBasePath());
5066
}
5167

68+
/**
69+
* Get vendor paths based on vendor_path section. Section allows to specify multiple vendor paths
70+
* separated by colon (":").
71+
*
72+
* @return List of vendor paths
73+
*/
5274
ImmutableList<Path> getVendorPaths() {
5375
Optional<ImmutableList<String>> vendorPaths =
54-
delegate.getOptionalListWithoutComments(SECTION, "vendor_path", ':');
76+
delegate.getOptionalListWithoutComments(SECTION, VENDOR_PATH, ':');
5577

5678
if (vendorPaths.isPresent()) {
5779
return vendorPaths.get().stream().map(Paths::get).collect(ImmutableList.toImmutableList());
5880
}
5981
return ImmutableList.of();
6082
}
6183

84+
/**
85+
* Get test main generator. The tool is a middle-step utility that utilizes selected .go sources
86+
* and generates the main.go which is later on compiled and used as test binary (run by "buck
87+
* test").
88+
*
89+
* @return test_main_gen tool
90+
*/
6291
Optional<Tool> getGoTestMainGenerator(BuildRuleResolver resolver) {
63-
return delegate.getView(ToolConfig.class).getTool(SECTION, "test_main_gen", resolver);
92+
return delegate.getView(ToolConfig.class).getTool(SECTION, TEST_MAIN_GEN, resolver);
93+
}
94+
95+
/**
96+
* Get "project_path" location. When set, buck project will copy generated sources to given
97+
* directory
98+
*
99+
* @return project_path path
100+
*/
101+
Optional<Path> getProjectPath() {
102+
Optional<String> path = delegate.getValue(SECTION, PROJECT_PATH);
103+
return (path.isPresent()) ? Optional.of(Paths.get(path.get())) : Optional.empty();
64104
}
65105
}

src/com/facebook/buck/features/go/GoProjectCommandHelper.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public class GoProjectCommandHelper {
7474
private final Console console;
7575
private final ListeningExecutorService executor;
7676
private final Parser parser;
77+
private final GoBuckConfig goBuckConfig;
7778
private final BuckConfig buckConfig;
7879
private final Cell cell;
7980
private final boolean enableParserProfiling;
@@ -92,6 +93,7 @@ public GoProjectCommandHelper(
9293
this.console = projectGeneratorParameters.getConsole();
9394
this.executor = executor;
9495
this.parser = projectGeneratorParameters.getParser();
96+
this.goBuckConfig = new GoBuckConfig(params.getBuckConfig());
9597
this.buckConfig = params.getBuckConfig();
9698
this.cell = params.getCell();
9799
this.enableParserProfiling = enableParserProfiling;
@@ -247,9 +249,9 @@ private void copyGeneratedGoCode(
247249
Path vendorPath;
248250
ProjectFilesystem projectFilesystem = cell.getFilesystem();
249251

250-
Optional<String> vendorConfig = buckConfig.getValue("go", "vendor_path");
251-
if (vendorConfig.isPresent()) {
252-
vendorPath = Paths.get(vendorConfig.get());
252+
Optional<Path> projectPath = goBuckConfig.getProjectPath();
253+
if (projectPath.isPresent()) {
254+
vendorPath = projectPath.get();
253255
} else if (projectFilesystem.exists(Paths.get("src"))) {
254256
vendorPath = Paths.get("src", "vendor");
255257
} else {
@@ -259,15 +261,27 @@ private void copyGeneratedGoCode(
259261
Preconditions.checkNotNull(getActionGraph(targetGraphAndTargets.getTargetGraph()));
260262
DefaultSourcePathResolver sourcePathResolver =
261263
DefaultSourcePathResolver.from(new SourcePathRuleFinder(result.getActionGraphBuilder()));
264+
265+
// cleanup files from previous runs
262266
for (BuildTargetSourcePath sourcePath : generatedPackages.keySet()) {
263267
Path desiredPath = vendorPath.resolve(generatedPackages.get(sourcePath));
264-
projectFilesystem.mkdirs(desiredPath);
265-
for (Path path : projectFilesystem.getDirectoryContents(desiredPath)) {
266-
if (projectFilesystem.isFile(path)) {
267-
projectFilesystem.deleteFileAtPath(path);
268+
269+
if (projectFilesystem.isDirectory(desiredPath)) {
270+
for (Path path : projectFilesystem.getDirectoryContents(desiredPath)) {
271+
if (projectFilesystem.isFile(path)) {
272+
projectFilesystem.deleteFileAtPath(path);
273+
}
268274
}
275+
} else {
276+
projectFilesystem.mkdirs(desiredPath);
269277
}
278+
}
279+
280+
// copy files generated in current run
281+
for (BuildTargetSourcePath sourcePath : generatedPackages.keySet()) {
282+
Path desiredPath = vendorPath.resolve(generatedPackages.get(sourcePath));
270283
Path generatedSrc = sourcePathResolver.getAbsolutePath(sourcePath);
284+
271285
if (projectFilesystem.isDirectory(generatedSrc)) {
272286
projectFilesystem.copyFolder(generatedSrc, desiredPath);
273287
} else {
@@ -294,9 +308,7 @@ private Map<BuildTargetSourcePath, Path> findCodeGenerationTargets(
294308
Path pkgName =
295309
packageNameArg
296310
.map(Paths::get)
297-
.orElse(
298-
Paths.get(buckConfig.getValue("go", "prefix").orElse(""))
299-
.resolve(targetNode.getBuildTarget().getBasePath()));
311+
.orElse(goBuckConfig.getDefaultPackageName(targetNode.getBuildTarget()));
300312
generatedPackages.putAll(
301313
((HasSrcs) constructorArg)
302314
.getSrcs()

test/com/facebook/buck/features/go/GoProjectIntegrationTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public void testBuckProjectPurgeExistingVendor() throws IOException {
6464
assertFalse(workspace.resolve("vendor/a/b0.go").toFile().exists());
6565
assertTrue(workspace.resolve("vendor/a/b1.go").toFile().exists());
6666
assertTrue(workspace.resolve("vendor/a/b2.go").toFile().exists());
67+
assertTrue(workspace.resolve("vendor/a/c1.go").toFile().exists());
68+
assertTrue(workspace.resolve("vendor/a/c2.go").toFile().exists());
6769
assertTrue(workspace.resolve("vendor/a/b/b.go").toFile().exists());
6870
}
6971
}

test/com/facebook/buck/features/go/testdata/go_project_with_existing_vendor/BUCK.fixture

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ go_library(
1919
package_name = "a",
2020
srcs = [
2121
":b",
22+
":c=c1.go",
23+
":c=c2.go",
2224
],
2325
)
2426

@@ -27,3 +29,22 @@ genrule(
2729
out = ".",
2830
cmd = "echo 'package a\nfunc A() {}\n' > $OUT/b1.go; echo 'package a\nfunc B() {}\n' > $OUT/b2.go;",
2931
)
32+
33+
# copy files from :c separately
34+
genrule(
35+
name = "c",
36+
out = ".",
37+
cmd = "echo 'package a\nfunc C() {}\n' > $OUT/c1.go; echo 'package a\nfunc D() {}\n' > $OUT/c2.go;",
38+
)
39+
40+
genrule(
41+
name = "c=c1.go",
42+
out = "c1.go",
43+
cmd = "mkdir -p `dirname $OUT` && cp $(location :c)/c1.go $OUT",
44+
)
45+
46+
genrule(
47+
name = "c=c2.go",
48+
out = "c2.go",
49+
cmd = "mkdir -p `dirname $OUT` && cp $(location :c)/c2.go $OUT",
50+
)

0 commit comments

Comments
 (0)