Repository navigation
Conversation
bradfitz
force-pushed
the
dumpsrc
branch
2 times, most recently
from
November 19, 2021 06:06
b9e03f8 to
76b1486
Compare
Contributor
|
What's the status/plan here? Should I review? |
josharian
suggested changes
Dec 14, 2021
josharian
left a comment
Contributor
There was a problem hiding this comment.
Using the "request changes" button so that GitHub stops nagging at me that it's my turn on this PR.
Refactor dumpSource to return *txtar.Archive and error instead of printing debug output to stdout. Each source file in the walked packages becomes a txtar file entry with module-relative paths. Filter output to only include packages within the target module, skipping stdlib and external dependencies. Remove all debug logging (DEAD, live symbols, offsetRange, commented prints) and the debug fmt.Printf calls (### PACKAGE, file.go, etc). Replace deprecated ioutil calls with os equivalents (os.ReadFile, os.WriteFile, os.CreateTemp). Add TestDumpSource with golden file comparison using testdata/<binname>_<goos>_<goarch>.txtar and an -update-golden flag.
When -dumpsrc is given a path to a file on disk instead of a package name, read its embedded build info (debug/buildinfo) to discover the main package path, module version, and GOOS/GOARCH. Create a temp workspace with pinned dependencies, download the module source, and rebuild with -gcflags=all=-N -l so nm sees all live symbols. Binaries built from local/dirty sources are rejected with a clear error since their version can't be resolved from the module proxy. Also: - Split buildGenLive into nmLive (just nm) and buildGenLive (build+nm) - Add goos/goarch/dir fields to walker for cross-platform support - Fix latent bug: scan.Err() was returning wrong variable - Bump go.mod to go 1.18 for debug/buildinfo - Add dumpsrc-TODO.md documenting known gaps
Update golang.org/x/tools to v0.44.0, github.com/pkg/diff to latest, and indirect deps. The old x/tools (Dec 2020) panicked with Go 1.26's type checker internals.
- Remove module filter so all dependencies are included in output, not just the root module's packages - Prefix file names with their module path (e.g. "golang.org/x/tools/txtar/archive.go") - Show stdlib files as "go-1.26.1/src/..." instead of absolute paths - Sort all files by name in the txtar output
Branches guarded by constant expressions like runtime.GOOS == "android" should be eliminated when the target GOOS is known, so users don't see irrelevant platform-specific code paths.
Skip files that have no top-level declarations after dead code stripping (only comments, package clause, and imports). This removes noise like stub files that exist only for build constraints.
Before walking each file's AST, scan the package for method declarations with live symbols and mark their receiver types as live too. This fixes FooType getting stripped while FooType.ValueMethod and (*FooType).PtrMethod were kept.
Run goimports (golang.org/x/tools/imports) on stripped source instead of just gofmt, so imports that become unused after dead code removal are cleaned up. Falls back to format.Source if goimports fails. Add import "fmt" to test/smallbin used only by dead function Bar, to verify the import is removed in the output.
Add test/minifmt, a tiny fmt-like package with no external deps. Use it in smallbin's dead Bar function instead of stdlib fmt, so the golden file stays small while still testing that unused imports are removed after dead code stripping.
Evaluate if-statements that compare runtime.GOOS or runtime.GOARCH against string literals. When the condition is statically known (e.g. runtime.GOOS == "android" on a linux build), strip the dead branch and keep only the live one. Handles ==, !=, and if/else forms. Default w.goos/w.goarch to runtime.GOOS/runtime.GOARCH when not explicitly set, so the target platform is always known. Add test/platbin with runtime.GOOS branches and a programmatic test (TestDumpSourceGOOSDCE) that verifies android branches are stripped without needing a large golden file.
Use pkg.TypesInfo to evaluate constant boolean expressions in if-statement conditions, stripping dead branches. This handles user-defined constants like `const debug = false` in addition to the existing runtime.GOOS/GOARCH special cases. Add NeedTypesInfo to the packages.Load mode so TypesInfo is populated. For bare identifier constants (e.g. `if debug`), look up the const value via TypesInfo.Uses since TypesInfo.Types excludes identifiers. Add const debug=false test case to test/platbin.
Make evalBoolConst recursive so it handles logical operators with partial constant evaluation: - false && anything → false (strip the if body) - true || anything → true (strip the else) - !constBool → negate - parenthesized expressions This handles patterns like "if debug && v.someMethod()" where debug is a false const — the whole condition short-circuits to false. Add test cases: ConstAnd (false && method), NotConstAnd (!false && GOOS check), ConstOr (!false || GOOS check).
The AST walker was applying edits directly to the edit buffer during traversal, which could panic when byte offsets exceeded the source file length. This happened with cgo packages where pkg.Syntax corresponds to CompiledGoFiles (which can differ from GoFiles), and could also happen when multiple edit types overlapped (e.g. an if-stmt DCE inside a dead function deletion). Fix by: - Using pkg.CompiledGoFiles instead of pkg.GoFiles to read source, since pkg.Syntax corresponds to CompiledGoFiles - Collecting all edits during the AST walk into a pending list - Filtering out-of-bounds edits and edits fully contained within larger edits before applying them in a single pass Add test/overlapbin with a dead function containing a const-guarded if-statement, exercising the overlapping edit case.
Add -cgo flag accepting "", "true", or "false". Empty string (default) leaves CGO_ENABLED to the Go toolchain's auto-detection. Previously the process() path hardcoded CGO_ENABLED=1. Wire the flag through to both the depaware dependency analysis and the dumpsrc paths. For dumpSourceBinary, read CGO_ENABLED from the binary's embedded build settings.
CompiledGoFiles can include cgo-generated files stored in the build cache (e.g. /home/user/.cache/go-build/...-d). These aren't real source files and shouldn't appear in the txtar output.
Look up GOROOT from the target go toolchain via "go env GOROOT" instead of using runtime.GOROOT() (which reflects the depaware binary's own toolchain, not the one being used for the target). Normalize stdlib paths to "GOROOT/src/..." without version numbers. This handles cases where the go binary is a different toolchain (e.g. tsgo with a different GOROOT path).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.