Skip to content

Export: trimming enhancements - #3530

Open
DuBento wants to merge 121 commits into
thought-machine:masterfrom
DuBento:export-from-package-metadata
Open

Export: trimming enhancements#3530
DuBento wants to merge 121 commits into
thought-machine:masterfrom
DuBento:export-from-package-metadata

Conversation

@DuBento

@DuBento DuBento commented May 1, 2026

Copy link
Copy Markdown
Contributor

Enhancements to plz export, moving from a basic target-level trimming (using gc.RewriteFile) to build statement-level trimming, including only the required build rules and subincludes.
For consistency, we format all the exported BUILD files.

Changelog:

  • Introduced PackageMetadata to track the relationship between BUILD file statements, the targets they generate, and the subincludes they require. Made optional to avoid the overhead for most ops and enabled for the export.
  • Introduced ScopeMetadata to track object origins (subincluded labels) and statement tracking during the interpreter phase. Made optional to avoid the overhead for most ops and enabled for the export.
  • Refactored src/export/export.go to enforce better separation of the DefaultExporter (for trimming) and NoTrimExporter.
  • Added logic to parse BUILD files and selectively write back statements based on whether the generated targets are part of the export set.
  • Implemented "minimal subinclude" generation, which rewrites subinclude() calls to only include labels actually used by the exported targets.
  • Update and added some of the e2e to reflect the changes in implementation.
  • Moved trimming from a GC-based target removal to a full AST statement walk.

duarte added 30 commits April 29, 2026 19:28
setting subincludes at package level instead of at target level
- register subinclude statements in the package metadata
- filter subincludes label
- export all non build_target related statements
…dary build def with sources

the updated test uses non-standard child naming to validate new trimming logic
DuBento added 24 commits June 17, 2026 18:28
…a to also avoid tracking inside subincludes
…Parser is set.

This wasn't an issue before since we would force close all the channels and fail during the first parse, but now we will attempt to wait for parsed package during an export.
…ents (including non-target generators).

The reason for pivoting is that the previous implementation failed to identify necessary subincludes for variable declarations or other builtin methods (string join)
…ls to interpretStatements with the package level scope (e.g. for loop)
…y if blocks that have interpreted stmts (var redeclaration)

@toastwaffle toastwaffle 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.

Nearly there!

As ever, comments phrased as questions probably imply a need for code comments

Comment thread src/core/build_target.go
Comment thread src/core/build_target.go Outdated
Comment thread src/core/package_metadata.go Outdated
return int64(bs.Start)
}

// hashBuildStatement mixes the Start and End byte coordinates to produce a unique 64-bit hash.

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.

I feel like for amusement we should state that "This does introduce a 4,294,967,296 byte size limit on BUILD files processed by Please"

Also, is it at all concerning that our hash is not uniformly distributed? What's worse - a non-uniform distribution, or doing more work to make it uniform?

Comment thread src/core/package_metadata.go Outdated
Comment thread src/core/package_metadata.go Outdated
@@ -0,0 +1,2 @@
[parse]
BuildFileName = BUILD_FILE No newline at end of file

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.

supernit: missing trailing newline

custom_file(
name = "unneeded",
src = "unneeded.txt",
) No newline at end of file

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.

supernit: missing trailing newline

@@ -0,0 +1,2 @@
[parse]
BuildFileName = BUILD_FILE No newline at end of file

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.

supernit: missing trailing newline

name = "_foo#sub",
srcs = ["BUILD_FILE"],
visibility = ["PUBLIC"],
) No newline at end of file

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.

supernit: missing trailing newline

@@ -0,0 +1,2 @@
[parse]
BuildFileName = BUILD_FILE No newline at end of file

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.

supernit: missing trailing newline

@peterebden peterebden left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I have some worries about crossing package responsibilities here - I get there's a lot more information we need to store to support this, and we can work through a bunch of that, but I think some of these changes like wanting to request parses from code post the actual build is a line we shouldn't cross (and I think maybe we don't have to).

Comment thread src/core/state.go
// is when exporting dependencies of targets that are not explicitly used but adjacent/related.
KeepParserRunning bool
// WaitForDisplay is a function that blocks until the display thread has finished.
WaitForDisplay func()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This feels very out of place here. I don't think the build state should need to know about this.

Comment thread src/core/state.go
// calls to the parser. This is needed to support the export operation since the export logic will
// attempt to export targets that have not been parsed during the normal build phase. An example
// is when exporting dependencies of targets that are not explicitly used but adjacent/related.
KeepParserRunning bool

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm really uncomfortable about this. It feels very strange to keep things around post the actual build running. I'm also not clear how it can really work, since in general the parser needs to be able to request further builds, so you don't just need the parser running, you need the whole build 'engine' ready to go again.

IIUC the requirement is for export to be able to request additional parses of things 'alongside' in the same BUILD file but weren't original targets. I think we could solve this more simply by make it parse all those things in the first place - the faster version is to convert all initial labels to :all, the dumber version is to just parse everything for export in the first place, so we know the graph is complete when we get there.

Comment on lines +12 to +15
// BuildStatement represents the start and end byte positions of a parsed statement in a BUILD file.
type BuildStatement struct {
Start, End int
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This isn't fundamentally bad, but it does all feel like something that core doesn't currently deal with.

I guess we do need it sitting on Package really, but I wonder if it'd be better being an unimplemented interface in this package, and having other packages that know about BUILD file source stuff implement it.

] + [f'plz --repo_root="{exported_repo}" {cmd}' for cmd in cmd_on_export]

test_cmd = [cmd.replace("plz ", "$TOOLS_PLEASE ") for cmd in test_cmd]
cache_args = '--override=cache.dir:"$($TOOLS_PLEASE query reporoot)"/plz-out/test-cache'

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is very fragile. I ran into corruption of this directory fairly quickly - presumably due to multiple tests running simultaneously that fought over it.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think it might be reasonable to add some locking to the cache so separate processes exclude each other, which should fix this. I can look at that separately.

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