Skip to content

build: prototype source patches for stdlib compat#1758

Open
cpunion wants to merge 25 commits intogoplus:mainfrom
cpunion:feat/source-patch-proto
Open

build: prototype source patches for stdlib compat#1758
cpunion wants to merge 25 commits intogoplus:mainfrom
cpunion:feat/source-patch-proto

Conversation

@cpunion
Copy link
Copy Markdown
Collaborator

@cpunion cpunion commented Mar 28, 2026

Summary

  • add a load-time source patch mechanism as a third stdlib compatibility path alongside existing alt patches
  • keep the rollout intentionally narrow: only iter is routed through source patch in this PR
  • preserve source-patch diagnostics, honor build tags and GOVERSION, and resolve the target GOROOT from go env

What This PR Includes

  • source patch package registration in runtime/build.go
  • load-time source patch assembly in internal/build
  • declaration-level patch behavior:
    • patch files are active by default for source-patched packages
    • //llgo:skipall
    • //llgo:skip ...
    • same-name declarations in patch files override original stdlib declarations
  • diagnostics handling:
    • injected patch files carry //line mappings back to the original patch files
    • filtered stdlib files preserve line structure as much as possible
  • source patch environment handling:
    • use go env GOROOT instead of the builder binary's compiled-in runtime.GOROOT()
    • skip source patching when the target stdlib package does not exist or is unreadable in the selected toolchain
  • tests covering routing, directives, diagnostics, missing-package behavior, and build-tag selection

Current Scope

  • iter is the only package switched from alt patch to source patch in this PR
  • sync/atomic, unique, internal/reflectlite, internal/runtime/maps, and internal/runtime/sys are kept on their existing non-source-patch paths
  • this PR is mechanism-first; broader package migration should be incremental and validated package by package

Proposal

Validation

  • go test ./internal/build -run '^(TestBuildSourcePatchOverlayForIter|TestIterUsesSourcePatchInsteadOfAltPkg|TestSyncAtomicRemainsAltPkg|TestInternalRuntimeMapsRemainsAltPkg|TestInternalRuntimeSysRemainsAltPkg|TestApplySourcePatchForPkg_Directives|TestApplySourcePatchForPkg_MissingStdlibPkg|TestApplySourcePatchForPkg_BuildTaggedPatch|TestApplySourcePatchForPkg_UnreadableStdlibPkg)$' -count=1
  • go test ./cl -run 'TestFromTestlibgo/atomic$|TestRunFromTestlibgo/atomic$' -count=1
  • LLGO_BUILD_CACHE=0 go run ./cmd/llgo test ./test/std/iter ./test/std/slices ./test/std/maps ./test/std/bytes ./test/std/strings
  • LLGO_BUILD_CACHE=0 go run ./cmd/llgo test ./test/std/sync/atomic

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a source patching mechanism for the LLGo compiler, allowing for the modification of standard library and other packages during the build process via an overlay. It includes logic to parse specific directives like //llgo:skipall and //llgo:nopatch to manage code overrides and stubs. Feedback includes a suggestion to improve the robustness of receiver type parsing by handling unexpected types with a panic and a recommendation to use more idiomatic Go for a package existence check.

@xgopilot
Copy link
Copy Markdown
Contributor

xgopilot bot commented Mar 28, 2026

Well-structured PR introducing a clean source-patch mechanism as a more maintainable alternative to alt-package replacements. The directive system (nopatch/skipall/skip) is elegant, and tests are thorough. A few code-quality issues below — one likely bug in sanitizeSourcePatchDirectiveLines, a duplicate-span logic error, and some dead code to clean up.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 28, 2026

Codecov Report

❌ Patch coverage is 62.50000% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.09%. Comparing base (2075aa9) to head (7424944).
⚠️ Report is 13 commits behind head on main.

Files with missing lines Patch % Lines
internal/env/env.go 62.50% 5 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1758      +/-   ##
==========================================
- Coverage   93.16%   93.09%   -0.08%     
==========================================
  Files          48       48              
  Lines       13349    13404      +55     
==========================================
+ Hits        12437    12478      +41     
- Misses        726      739      +13     
- Partials      186      187       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@cpunion cpunion force-pushed the feat/source-patch-proto branch from 552effe to bd685f8 Compare March 29, 2026 08:50
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.

1 participant