Skip to content
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

Plug gazelle_cabal.ImportData in Resolve to allow both gazelle extensions to run in the same gazelle_binary #41

Closed
wants to merge 17 commits into from

Conversation

GuillaumeGen
Copy link
Contributor

An attempt to solve #40
This applies the methodology suggested as the third item in this answer #40 (comment)

Now, gazelle_haskell_modules depends of gazelle_cabal, allowing us to pattern match on the type of imports in the Resolve function.

// This happens only when one run gazelle_cabal and gazelle_haskell_modules in one pass as the the gazelle_binary.
case gazelle_cabal.ImportData:
// We fully generate the rule that gazele_cabal would have produced.
gazelle_cabal.RunResolve(c, ix, rc, r, expr, from)
Copy link
Member

Choose a reason for hiding this comment

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

I need some help understanding the flow. Is this case entered anytime a haskell_binary/library/test rule is updated or created by gazelle_cabal?

If so, how are haskell_module rules generated from this rule?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This case is entered whenever the Resolve function is called on rules that come from the same gazelle run (so rules where the imports where constructed by gazelle_cabal and not by the haskell lib himportscan included in gazelle_haskell_modules).
Regarding the construction of haskell_modules rules, it is performed just like for other rules by setNonHaskellModuleDeps.

Copy link
Member

Choose a reason for hiding this comment

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

This code is calling RunResolve, which apparently is a duplicate of the Resolve callback of gazelle_cabal. Isn't that callback invoked by gazelle implicitly?

// (this field is either non-existent or filled with data from an outdated run),
// hence we can safely put an empty map here.
Modules: make(map[label.Label]bool),
Srcs: srcs,
Copy link
Member

Choose a reason for hiding this comment

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

Is this duplicating the code in addNonHaskellModuleRules that creates HRuleImportData for preexisting non-haskell_module rules?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it is. Is there any tips to discover the already implemented functions in our projects? Furthermore, I am not sure I understand clearly what the various arguments of this function represent.

Copy link
Member

@facundominguez facundominguez Oct 7, 2022

Choose a reason for hiding this comment

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

Is there any tips to discover the already implemented functions in our projects?

I don't think so, other than reading the code :)

Furthermore, I am not sure I understand clearly what the various arguments of this function represent.

Here's an attempt at explaining them:

func addNonHaskellModuleRules(
	c *Config,
	pkgRoot string,
	repo string,
	pkg string,
	gen language.GenerateResult,
	rules []*rule.Rule,
) language.GenerateResult
  • c is the same config parameter as given to Resolve
  • pkgRoot root of the project tree
  • repo is the name of the bazel repo
  • pkg is the package path inside project tree
  • gen is the container to which new generated rules are added
  • rules are the rules already present in the BUILD file.

There is this invocation that may give further clues on how the arguments are created. Probably we won't need all of these arguments after factoring out the piece that is needed by both addNonHaskellModuleRules and Resolve.

@facundominguez
Copy link
Member

This is missing a test, right?

@GuillaumeGen
Copy link
Contributor Author

GuillaumeGen commented Oct 7, 2022

This is missing a test, right?

It is, I would like to be able to run the current examples before creating new ones.

}
}
// After this cleaning, the new deps are set.
setNonHaskellModuleDeps(&hmc, c.RepoRoot, ix, r, newImports, from)
Copy link
Member

Choose a reason for hiding this comment

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

Why are the attributes deleted from the old rule?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for the very delayed answer.
Those attributes are deleted because Resolve is not a pure function, it alters the rules given to it. What I really want to do is to replace r by newr, but this not possible (in the sense that I do whatever is required on newr, this will not impact r and in the end, newr won't exist outside of the scope of this function). So I have to copy newr into r and for this, I use my knowledge that the difference between the 2 is the existence of some attributes to do it by deletion rather than doing a "deep copy".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definitely worth adding a comment.

name = "io_tweag_gazelle_cabal",
strip_prefix = "gazelle_cabal-240ad1af8d0d0ca5c3fd3ff7afcdb2f0fe0dbbe2",
urls = ["https://github.com/tweag/gazelle_cabal/archive/240ad1af8d0d0ca5c3fd3ff7afcdb2f0fe0dbbe2.zip"],
)
Copy link
Member

@facundominguez facundominguez Oct 11, 2022

Choose a reason for hiding this comment

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

It is a unsatisfying that we now have to bring gazelle_cabal even if the user does not intend to use it. But I don't see how to avoid it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree

@facundominguez
Copy link
Member

For now, I'm closing the PR for considerations made here.

But I think this problem would be an opportunity to follow up with gazelle devs to make the integration of extensions easier.

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.

2 participants