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

Added support to expose imports for python to Skylark. #2791

Closed
wants to merge 8 commits into from

Conversation

srikalyan
Copy link

Fixes #2617. Please let me know if you need anything else.

@bazel-io
Copy link
Member

bazel-io commented Apr 7, 2017

Can one of the admins verify this patch?

@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed, please reply here (e.g. I signed it!) and we'll verify. Thanks.


  • If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address. Check your existing CLA data and verify that your email is set on your git commits.
  • If you signed the CLA as a corporation, please let us know the company's name.

@srikalyan
Copy link
Author

I signed it!

@googlebot
Copy link

CLAs look good, thanks!

@googlebot googlebot added cla: yes and removed cla: no labels Apr 7, 2017
// Python scripts start with x.runfiles/ as the module space, so everything must be manually
// adjusted to be relative to the workspace name.
packageFragment = PathFragment.create(ruleContext.getWorkspaceName())
.getRelative(packageFragment);
return PathFragment.create(ruleContext.getWorkspaceName())
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this the same as PackageIdentifier#getRunfilesPath()?

Copy link
Author

Choose a reason for hiding this comment

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

Removed it.

@@ -60,6 +60,11 @@ void collectDefaultRunfilesForBinary(RuleContext ruleContext, Runfiles.Builder b
RuleContext ruleContext, Collection<Artifact> sources, PyCommon common);

/**
* @return - {@link PathFragment} representing the python package.
*/
PathFragment getPackageFragment(RuleContext ruleContext);
Copy link
Contributor

Choose a reason for hiding this comment

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

Likewise, there is no need to put this here; since it's just PackageIdentifier#getRunfilesPath(), let's just use that and not pollute the PythonSemantics interface (which shouldn't exist anyway...)

Copy link
Author

Choose a reason for hiding this comment

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

Removed it.

return NativeClassObjectConstructor.STRUCT.create(
ImmutableMap.<String, Object>of(
TRANSITIVE_PYTHON_SRCS,
SkylarkNestedSet.of(Artifact.class, transitivePythonSources),
IS_USING_SHARED_LIBRARY,
isUsingSharedLibrary),
isUsingSharedLibrary,
PACKAGE_FRAGMENT,
Copy link
Contributor

Choose a reason for hiding this comment

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

Given that this is computable from the package fragment, how about not having this function here? If needed, we could add a method to Label that tells its runfiles path, right, @laurentlb ?

Copy link
Author

Choose a reason for hiding this comment

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

Removed it.

@iirina
Copy link
Contributor

iirina commented May 23, 2017

@srikalyan can you follow up on this?

@srikalyan
Copy link
Author

@iirina I followed up couple days back. Please let me know if I am missing something.

@iirina
Copy link
Contributor

iirina commented May 23, 2017

I didn't see any replies to @lberki's comments and I overlooked the last commit, sorry. Can you mark the comments as done?
@lberki, can you take another look at this?

Copy link
Author

@srikalyan srikalyan left a comment

Choose a reason for hiding this comment

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

Addressed all the review comments.

@iirina
Copy link
Contributor

iirina commented May 23, 2017

Thank you!

isUsingSharedLibrary,
IMPORTS,
SkylarkNestedSet.of(String.class,
makeImportStrings(imports ))
Copy link
Contributor

Choose a reason for hiding this comment

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

super nit: unnecessary space after "imports"

Copy link
Author

Choose a reason for hiding this comment

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

Done

@lberki
Copy link
Contributor

lberki commented May 24, 2017

Reviewed.

@bazel-io
Copy link
Member

Can one of the admins verify this patch?

final NestedSetBuilder<String> builder = NestedSetBuilder.compileOrder();

for (PathFragment pathFragment : imports) {
builder.add(pathFragment.toString());
Copy link
Contributor

Choose a reason for hiding this comment

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

This effectively negates any benefit of nested sets here, because you create a new, flat nested set with the strings instead of the carefully built-up nested set with import path fragments inherited from dependent targets.

@laurentlb @vladmos , is there a way to expose a NestedSet<PathFragment> to Skylark instead? That's a more promising avenue than working around the limitation that PathFragments aren't available there. This would also help with flattening the PathFragments into strings so that e.g. "longdirectory/a" and "longdirectory/b" can share structure.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry for late reply. We do not plan to expose PathFragments to Skylark. Can you do with NestedSet<String> instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

Or return a Iterable that lazily iterates a nested set when needed.

Copy link
Author

Choose a reason for hiding this comment

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

Sorry for my late reply. I will add it asap.

Copy link
Author

Choose a reason for hiding this comment

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

I am sorry for not understanding what you are trying to suggest here. I think (I may be completely wrong) that you are suggesting the proposed change? @dslomov can you comment?

Copy link
Contributor

Choose a reason for hiding this comment

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

Friendly ping @dslomov :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Please read https://docs.bazel.build/versions/master/skylark/depsets.html.

What you are doing here is flattening the depset of path fragments to convert them to strings.
Since this is happening for every python rule, that will bloat the memory.
There are two approaches you can take instead:

  1. Use NestedSet<String> instead of NestedSet<PathFragment> everywhere.
  2. Make a value of 'imports' an Iterable that lazily flattens a nested set on iteration.

My preferred approach is (1) but I do not know how expensive it is to keep Strings instead of PathFragments.
(2) is ok as well.

PythonSemantics semantics, NestedSet<Artifact> filesToBuild) {

public void addCommonTransitiveInfoProviders(
RuleContext ruleContext,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you need RuleContext here?

Copy link
Author

Choose a reason for hiding this comment

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

I don't exactly remember as this has been really old. I think I had to add it because rule context in pycommon was different from pylibrary/pybinary.

Copy link
Author

Choose a reason for hiding this comment

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

I can double check this and let you know.

Copy link
Author

Choose a reason for hiding this comment

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

Removed it.

final NestedSetBuilder<String> builder = NestedSetBuilder.compileOrder();

for (PathFragment pathFragment : imports) {
builder.add(pathFragment.toString());
Copy link
Contributor

Choose a reason for hiding this comment

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

Or return a Iterable that lazily iterates a nested set when needed.

@hlopko
Copy link
Member

hlopko commented Oct 11, 2017

Friendly ping @srikalyan , is this still being developed?

@bazel-io
Copy link
Member

Can one of the admins verify this patch?

@ali5h
Copy link

ali5h commented Dec 8, 2017

any update on this?

@fahhem
Copy link
Contributor

fahhem commented Dec 12, 2017

Any chance this is getting in in 2017?

@srikalyan
Copy link
Author

@fahhem I am waiting on the review comments

@fahhem
Copy link
Contributor

fahhem commented Dec 19, 2017

@srikalyan, yes, I was hoping to prod the bazel maintainers more than you. Sorry for that! @dslomov or @vladmos, ping! Is there a plan to make something more general available soon or can we merge this PR for now? Unfortunately, we missed the 0.9.0 release, but hopefully we can get in before the next one still!

@jin
Copy link
Member

jin commented Jan 8, 2018

@dslomov PTAL at the review comments when you have the bandwidth?

@philwo
Copy link
Member

philwo commented Feb 3, 2018

Ping @dslomov - please take action on this PR.

@bazel-io
Copy link
Member

bazel-io commented Feb 3, 2018

Can one of the admins verify this patch?

@laszlocsomor
Copy link
Contributor

ping -- are there any updates? can we merge or close this PR?

@srikalyan
Copy link
Author

I think it is safe to merge. I am just waiting for approval :(

final NestedSetBuilder<String> builder = NestedSetBuilder.compileOrder();

for (PathFragment pathFragment : imports) {
builder.add(pathFragment.toString());
Copy link
Contributor

Choose a reason for hiding this comment

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

Please read https://docs.bazel.build/versions/master/skylark/depsets.html.

What you are doing here is flattening the depset of path fragments to convert them to strings.
Since this is happening for every python rule, that will bloat the memory.
There are two approaches you can take instead:

  1. Use NestedSet<String> instead of NestedSet<PathFragment> everywhere.
  2. Make a value of 'imports' an Iterable that lazily flattens a nested set on iteration.

My preferred approach is (1) but I do not know how expensive it is to keep Strings instead of PathFragments.
(2) is ok as well.

@lfpino
Copy link
Contributor

lfpino commented Mar 27, 2018

Hi @srikalyan, Bazel sheriff here, what's the status of this pull request? It hasn't been updated for more than a week so it'd be good to understand if it's still ongoing or safe to close. Thanks.

@jin
Copy link
Member

jin commented Apr 9, 2018

@srikalyan ping, could you please address @dslomov's comments? thanks!

@c4urself
Copy link
Contributor

A lot of people are wasting cycles on this, trying to reimplement all the python rules in Skylark to be able to access their version of the imports attribute, see for example: https://github.com/TriggerMail/rules_pyz/blob/master/rules_python_zip/rules_python_zip.bzl#L39 anything we can do to move this forward?

@ulfjack
Copy link
Contributor

ulfjack commented Oct 11, 2018

Closing due to lack of updates from the original author. Maybe someone else wants to take this over.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.