Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Separate Parsing and Compiling #1361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 26, 2025
Merged

Conversation

grantnelson-wf
Copy link
Collaborator

@grantnelson-wf grantnelson-wf commented Feb 10, 2025

This is the start of several changes to get generic information propagated across package lines.

This changes how packages are loaded so that they are not converted into Archives right away. This does mean that Archive caching has been disabled. With the Archives creation delayed, we have two phases:

  1. Parse: The parse phase uses BuildPackages to determine the files that are needed to be parsed for a package and determine the imports that are required for a package. The result of the parse phase will be ParsedPackages. All the ParsedPackages needed for a project will be gotten prior to moving onto the compile phase.
  2. Compile: The compile phase will take the root ParsedPackage and start compiling it, compiling any imported ParsedPackage as it goes. Each compile of a ParsedPackage results in an Archive. Once the Archives have all been created, GopherJS continues as usual.

The point of this change is to prepare for following PR's. In the following PR's we can break up phase 2 into smaller steps. Right now the compile phase does Type Checking, code simplifying, link finding, anaylizing, then converting into JS. The analyzing includes determining flattening, blocking, etc information for the methods. At the point I left the TODO between phase 1 and 2. We can move several of those parts out of compile and do them to the whole project at once. We can run type checking and analyzing all at once to help propagate information across package lines. Once all the preliminary information has been gathered, then we output the Archives.

This is related to #1013 and #1270

@grantnelson-wf grantnelson-wf self-assigned this Feb 10, 2025
@grantnelson-wf grantnelson-wf force-pushed the delayCompile2 branch 2 times, most recently from 1a4dcc2 to 963a05a Compare February 13, 2025 21:11
@grantnelson-wf grantnelson-wf changed the title [WIP] Separate Parsing and Compiling Separate Parsing and Compiling Feb 13, 2025
@grantnelson-wf grantnelson-wf marked this pull request as ready for review February 13, 2025 23:06
Copy link
Member

@flimzy flimzy left a comment

Choose a reason for hiding this comment

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

Read through it all, and it looks good to me. But happy to have @nevkontakte look at it too.

@grantnelson-wf
Copy link
Collaborator Author

I was continuing onto the compile break up and realized that ParsedPackage is nearly identical to sources. Since this one is already ready to go, I'm going to leave it as is, but the next PR will remove ParsedPackage and make sources exported as Sources with the extra fields in ParsedPackage.

However, if you'd like, I could just make that change in this PR instead of having another one.

Copy link
Member

@nevkontakte nevkontakte left a comment

Choose a reason for hiding this comment

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

Not a specific request, but something to consider and/or use as a reference:

https://pkg.go.dev/golang.org/x/tools/go/packages#Load both loads package metadata and (optionally) parses AST files there too. So, at a first glance, adding parsed sources to PackageData is not a bad idea.

More broadly, I suspect that we could simplify a lot of what build package does today by leaning more on go/packages. My idealized build process would look something like:

  1. Use https://pkg.go.dev/golang.org/x/tools/go/packages#Load to load the entire program. Use the Confg.ParseFile hook to augment standard library on the fly [1].
  2. Perform GopherJS-specific analysis.
  3. Perform compilation.

I think a lot of custom BuildContext stuff would simply disappear after that. We don't have to do that right now, but I thought I'd mention it in case it turns out to be a more expedient way to achieve what we need to support generics.

[1]: Looks like this Config.ParseFile hook is new? I don't remember seeing it before, anyway. Its existence may make #1021 obsolete, or at least not a requirement for GopherJS to adopt go/packages.

@grantnelson-wf
Copy link
Collaborator Author

@nevkontakte (in response to this comment),

I had a WIP (this closed PR) where I was attempting to use golang.org/x/tools/go/packages#Load but was having trouble figuring out how to perform the augmentation during loading so that we could have Load perform the type checks on the augmented files with the augmented imports. I was using ParseFile but I think it was an issue with FileSet.

The main issue I was having seemed to be that FileSet didn't report enough bytes for the augmented file. The resulting augmented file could be longer than the original so we couldn't override the location with /token#File.AddLineColumnInfo. The experiment I wanted to try was to use a separate FileSet when loading the original and override files, then perform the augmentation, then create a "new file" for the FileSet that is the correct length for the augmented file. I just haven't been able to try it out to see if it fixes the issues I was hitting. I also think it could have been because of how I was trying to test it and we could avoid those issues without fixing the FileSet... I don't know yet.

Another thing I wanted to try, if I couldn't figure out the above, was to just load the packages as if not in GopherJS with packages#Load, then, after getting the packages back, augment the files for GopherJS, add the additional packages for GopherJS, and lastly run type checks on them.

Anyway, because I was having issues getting it to work, I decided to try to finish that later, if no one else has figured it out. However, I feel you are right, a lot of the Config stuff and stuff getting up to the ParsedPackages could be removed if we did that.

@nevkontakte
Copy link
Member

@grantnelson-wf thanks for the details! That all sounds reasonable. If I have some spare time one of the weekends, I may try to experiment with it myself. Perhaps I will be able to find a working solution, or perhaps prove definitively that it's not viable :)

@grantnelson-wf grantnelson-wf merged commit a3f0ab4 into gopherjs:master Feb 26, 2025
10 checks passed
@grantnelson-wf grantnelson-wf deleted the delayCompile2 branch February 26, 2025 16:44
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.

3 participants