Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

investigate using Go's -overlay flag to replace source files #578

Closed
mvdan opened this issue Sep 3, 2022 · 1 comment
Closed

investigate using Go's -overlay flag to replace source files #578

mvdan opened this issue Sep 3, 2022 · 1 comment

Comments

@mvdan
Copy link
Member

mvdan commented Sep 3, 2022

$ go help build | grep -A10 -- '-overlay file'
	-overlay file
		read a JSON config file that provides an overlay for build operations.
		The file is a JSON struct with a single field, named 'Replace', that
		maps each disk file path (a string) to its backing file path, so that
		a build will run as if the disk file path exists with the contents
		given by the backing file paths, or as if the disk file path does not
		exist if its backing file path is empty. Support for the -overlay flag
		has some limitations: importantly, cgo files included from outside the
		include path must be in the same directory as the Go package they are
		included from, and overlays will not appear when binaries and tests are
		run through go run and go test respectively.

This feature was mainly added for gopls, for the sake of supporting the use of commands like go list or go test on Go packages which are modified on an editor but not yet written to disk.

We currently write obfuscated files to temporary directories, and then replace the original paths fed to the toolchain with those temporary paths. This has worked OK for the most part, but it is also somewhat tricky. Here are some edge cases we've run into:

However, there is also one huge roadblock to using -overlay: it currently requires that we supply all file replacements upfront. Meaning that we would have to first load all the Go packages in a build, obfuscate them all at once, then perform the obfuscated build with one big -overlay JSON map. Which is certainly possible, but could also make some larger builds significantly slower due to the loss of incremental builds. Each of the three steps (initial load, obfuscation, obfuscated build) could itself be incremental and parallelized, but splitting the work into three steps would slow down many builds.

I think this is likely a dead end, given that we want to interactively and progressively obfuscate Go packages, not all of them at once, and that's not how -overlay works. But I thought laying out my thoughts here would be useful. I'll probably close this issue in a few weeks assuming I didn't miss anything.

We could request upstream to provide a form of -overlay that allows us to reactively provide file overlays as a build progresses, but I doubt that would be considered: it feels very niche, and someone who wants that much power can likely just use -toolexec like we do.

@mvdan
Copy link
Member Author

mvdan commented Sep 18, 2022

Closing per the above. It was interesting to think about whether we could use this flag, but I don't think we can without severely compromising on performance.

@mvdan mvdan closed this as not planned Won't fix, can't repro, duplicate, stale Sep 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant