Skip to content

Export: trimming enhancements - #3530

Merged
peterebden merged 155 commits into
thought-machine:masterfrom
DuBento:export-from-package-metadata
Sep 9, 2026
Merged

peterebden merged 155 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.

Comment thread src/core/package.go Outdated
Comment thread src/core/package.go Outdated
Comment thread src/core/package.go
Comment thread src/core/package.go Outdated
Comment thread src/core/package.go Outdated
Comment thread src/core/package_metadata.go Outdated
Comment thread src/core/package_metadata.go Outdated
Comment thread src/core/package_metadata.go Outdated
Comment thread src/core/package_metadata.go Outdated
Comment thread src/export/export.go Outdated
Comment thread src/export/export.go Outdated
state *core.BuildState
targetDir string

exportedTargets map[*core.Package]map[core.BuildLabel]bool

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.

Using a pointer as a key always makes me uncomfortable; can we avoid this?

More generally, could we avoid the nested map? core.BuildLabel includes the package name anyway, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We can probably avoid using the pointer has key by using the package label, but we will have to look up in the graph each time. I was using the pointer directly assuming some consistency of no repeated package instances. Should I use the string instead and lookup in the graph each time we want to use it?

The nested map is useful for looping though the exported target per each package (and for efficient verification of visited targets). What's your opinion, should I try to unnest?

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 think using the pointer is a bug waiting to happen, and I don't think fetching the package itself will be too expensive (it's a map lookup protected by an RWLock). We should probably export packageKey and tuse that.

The nesting makes sense to keep, but add a comment?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is comment refers to outdate code. I forgot to mention here but I did change this after your initial comment. It currently uses a build label directly and is not nested.

// exportedTargets maintains a record of the targets that have been exported so far.
exportedTargets map[core.BuildLabel]bool

This integrated well with the visited check and I've added an explicit visitedPackages map for the implementation that requires it. I'll assume this aligns with your opinion and will not action on the comment.

Comment thread src/export/export.go
Comment thread src/export/export.go Outdated
Comment thread src/export/export.go Outdated
Comment thread src/export/export.go Outdated
Comment thread src/export/export.go Outdated
Comment thread src/export/export_test.go
Comment thread src/export/export_test.go Outdated
Comment thread src/export/export_test.go Outdated
Comment thread src/parse/asp/interpreter.go Outdated
Comment thread src/parse/asp/objects.go Outdated
Comment thread src/parse/asp/interpreter.go Outdated
Comment thread src/parse/asp/interpreter.go Outdated
Comment thread src/parse/asp/targets.go Outdated
@DuBento DuBento changed the title Export: enhance trimming using build statement metadata Export: trimming enhancements Jun 5, 2026
Comment thread src/core/package_metadata.go Outdated
Comment thread src/core/package_metadata.go Outdated
Comment thread src/core/package_metadata.go Outdated
Comment thread src/core/package_metadata.go Outdated
Comment thread src/core/package_metadata.go
Comment thread src/parse/asp/interpreter.go Outdated
Comment thread src/parse/asp/interpreter_test.go Outdated
Comment thread src/plz/plz.go Outdated
Comment thread src/plz/plz.go Outdated
Comment thread src/please.go Outdated
@DuBento
DuBento force-pushed the export-from-package-metadata branch 4 times, most recently from f6fbf1a to 7152109 Compare June 17, 2026 10:01

@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
Comment thread src/core/package_metadata.go Outdated
Comment thread src/core/package_metadata.go Outdated
Comment thread test/export/test_subinclude_unused/source_repo/.plzconfig Outdated
Comment thread test/export/test_subinclude_unused/source_repo/BUILD_FILE Outdated
Comment thread test/export/test_subrepo_subtarget/expected_repo/.plzconfig Outdated
Comment thread test/export/test_subrepo_subtarget/source_repo/subrepo/BUILD_FILE Outdated
Comment thread test/export/test_subrepo_subtarget/source_repo/.plzconfig Outdated

@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 Outdated
Comment thread src/core/state.go Outdated
Comment thread src/core/package_metadata.go Outdated
Comment thread test/export/please_export_e2e_test.build_defs Outdated
duarte added 14 commits August 28, 2026 20:33
this is no longer relevant for the symbol tracking since we mirror the implicit logic for the actual scope symbols.
…arser alive

Replaces the fragile background-daemon "KeepParserRunning" parsing logic with a synchronous upfront-parsing design. When "ForceParseEntirePackage" is enabled, we queue and parse all other targets in a visited package exactly once at parse time.
@DuBento
DuBento force-pushed the export-from-package-metadata branch from 3f140a1 to 7552be7 Compare August 28, 2026 19:42
@DuBento

DuBento commented Aug 28, 2026

Copy link
Copy Markdown
Contributor Author

Comments addressed and ready for another review.

Comment thread src/core/build_label.go Outdated
Comment thread src/core/state_test.go Outdated
Comment thread src/core/state_test.go Outdated
Comment on lines +226 to +227
// Give the waiters time to block on the wait channel
time.Sleep(10 * time.Millisecond)

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 doesn't guarantee that they do start blocking

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've added some synchronisation to hopefully improve this, but I don't think we can avoid having a sleep. If you have any ideas, or prefer I remove the test, let me know.

d.printLines(targets)
for _, line := range cli.CurrentBackend.Output() {
d.printf("${ERASE_AFTER}%s\n", line)
logs := cli.CurrentBackend.Output()

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.

Maybe it's a bit late now but it would have been really nice to have changes like this split from the bulk of the export change here; it'd be easier to reason about a set of logging changes in isolation. I'm not really clear at the moment why you need to do this; I don't especially object to it but it doesn't seem like plz export has any particularly unique output requirements.

Comment thread src/parse/asp/builtins.go Outdated
Comment thread src/core/package_metadata.go Outdated
// to its StatementMetadata. Refer to [StatementMetadata] for more details but this single
// mapping tracks the targets produced by the statement, the subincluded labels required for its
// interpretation, and other information.
statements *cmap.Map[BuildStatement, *StatementMetadata]

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.

do you really need these to be cmaps? They are explicitly optimised for high concurrency but not low overhead, I didn't anticipate generating large numbers of them (in this case two per package).

Would a map-and-mutex not be adequate here? Or, from the comment, I'm a little unclear if the mutex is even required - they can only be written once and I assume you'd just be reading them later during the export operation?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We initially favoured the thread-safe type instead of a shared mutex. I eventually understood that the write phase is single threaded. We maintained the cmaps for consistency but I agree that it is a waste since it results in creating 8 maps + 8 mutexes per package and likely to include few objects in each map.
I believe it would be correct to have no locking whatsoever but I've refactored to include a RWlock for consistency and to be resistant to any future changes (or multi-threaded export). It's better than having cmaps and hopefully with minimal overhead for our current single threaded logic.

Comment thread src/core/package_metadata.go Outdated
Comment on lines +262 to +265
// The intention is to finds all the subincluded labels required by the package but not used to
// generate targets. An example could be a variable declaration that depends on a subincluded value.
// We range over all interpreted statements that require any subincluded target. From those, we
// filter out the statements that generate targets and any explicit subinclude() statement calls.

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.

Curious why you need this? I'm struggling a bit to see how this links to what's required for export to work

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is required to support the following examples (snippets from tests):

subinclude("//build_defs:versions_build_def")

for version, name in VERSIONS.items():
    pass  # Trimmed during export
for file in glob(["file*.in"]):
    genrule(
        name = "target_" + file.removesuffix(".in"),
        srcs = [file],
        outs = [file.removesuffix(".in") + ".out"],
        cmd = "cp $SRCS $OUT",
    )

Since we are not trimming variables or for headers, we need to determine what else is required by the BUILD file but doesn't necessarily generate a build target.

Comment thread src/core/package_metadata.go Outdated
Comment thread src/parse/asp/interpreter.go Outdated
Comment thread test/export/test_go_bin/source_repo/third_party/go/BUILD_FILE Outdated
Comment thread test/export/test_native_target_with_go_dep/source_repo/third_party/go/BUILD_FILE Outdated
Comment thread src/core/package_metadata.go Outdated
duarte added 3 commits September 8, 2026 19:15
this fixes an issue with running our //src/core:core export test on our CI runners that don't include the python binary
@peterebden
peterebden merged commit 611567b into thought-machine:master Sep 9, 2026
16 checks passed
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