Skip to content

refactor(compile): add trimpath to compile to remove local paths - #457

Open
sheldonhull wants to merge 7 commits into
magefile:masterfrom
sheldonhull:feat/add-trimpath-to-compile
Open

sheldonhull wants to merge 7 commits into
magefile:masterfrom
sheldonhull:feat/add-trimpath-to-compile

Conversation

@sheldonhull

Copy link
Copy Markdown
  • Include trimpath to ensure local filepaths from CI or developer don't get included in the produced artifact.
  • This was added in Go 1.13, so not sure if there is something special to handle adding this flag only if go version > 1.13.

Go documentation on this:

-trimpath
remove all file system paths from the resulting executable.
Instead of absolute file system paths, the recorded file names
will begin either a module path@version (when using modules),
or a plain import path (when using the standard library, or GOPATH).

- Include trimpath to ensure local filepaths from CI or developer don't get included in the produced artifact.
- This was added in Go 1.13, so not sure if there is something special to handle adding this flag only if go version > 1.13.
@jaredallard

jaredallard commented Feb 18, 2023 •

Copy link
Copy Markdown
Contributor

I feel like the aqua part should be called out in the title/desc somewhere....

EDIT: Ah, I see, that is from #451 and just wasn't removed when making this PR. I'd suggest cleaning that up :D

@sheldonhull

sheldonhull commented Feb 19, 2023 via email •

Copy link
Copy Markdown
Author

@sheldonhull

Copy link
Copy Markdown
Author

@jaredallard reverted back so pr changes are now just this. Not sure about compatibility for prior go versions to 1.13 like I mentioned but otherwise this will be good for privacy. Right now compiled binary includes full path. Cheers

Comment thread mage/main.go Outdated
sheldonhull and others added 2 commits February 24, 2023 03:36
Co-authored-by: Horacio Duran <horacio.duran@gmail.com>

@jaredallard jaredallard left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable to me :D

@natefinch natefinch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I applied the trimpath change to current master (0953947) and checked compilation, cross-compilation, deterministic generated output, and cache behavior. The main concern is the compatibility impact of making trimpath unconditional, rather than the flag implementation itself. The inline comment describes a reproduced source-relative asset lookup regression. Go-version availability is no longer a concern with the current Go 1.18 minimum.

Comment thread mage/main.go
gofiles[i] = filepath.Base(gofiles[i])
}
buildArgs := []string{"build", "-o", compileTo}
buildArgs := []string{"build", "-o", compileTo, "-trimpath"}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making -trimpath unconditional changes existing magefile behavior with no opt-out. Targets or helper libraries can use runtime.Caller to locate files relative to their source directory. I reproduced a program reading an adjacent asset via filepath.Join(filepath.Dir(file), "asset.txt"): the untrimmed build succeeds when run from another directory, while this build fails with open asset.txt: no such file or directory because the recorded source filename is no longer an absolute filesystem path. This is an inherent tradeoff of trimming paths, not a flag implementation bug. Could we make this opt-in, or provide an explicit opt-out and document the compatibility change, rather than changing all existing magefiles silently?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants