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

fix: Key construction in ExtractGoComments #112

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

candiduslynx
Copy link
Contributor

The intended (from README.md) usage of ExtractGoComments(base, path string, commentMap map[string]string) error treats the params as following:

  • base – package import path
  • path – package location on the filesystem
  • commentMap output

If we pass "../" as path param the code will fail to load comments properly because of the path.Join used on the unverified input. Moreover, if the path is the absolute path, the code will join it with base so we end up with keys base + "/" + absPath (cleaned in the path.Clean func, but still).

This PR fixes the behavior in the following way:

  • renaming path to rootPath for better understanding the semantics & not allowing name clashes in the func
  • creating a root variable equal to the absolute path of the rootPath location
  • walking the root path
  • trimming the root prefix of the path prior to joining with the base value

@candiduslynx
Copy link
Contributor Author

candiduslynx commented Oct 18, 2023

@samlown I think this actually fixes the intended use of AddGoComments as well (see the fixed test code as well).

See cloudquery/cloudquery#14647 for usage example with both vendored code & path that includes "../".

@candiduslynx
Copy link
Contributor Author

Hi @samlown!

Is there a chance to get this PR (and the other PRs I opened t this repo) reviewed & merged?

@samlown samlown added the needs tests Not enough tests for this to be accepted label Feb 19, 2024
"go/doc"
"go/parser"
"go/token"
"golang.org/x/exp/maps"
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure that we want to be using experimental stuff...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can copy the implementation as it's just a straightforward map[key]value -> []value func.

LMK if you think this is the way.

Copy link
Contributor

@samlown samlown left a comment

Choose a reason for hiding this comment

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

If I understand correctly, this proposal will break previous implementations, certainly the tests have changed suggesting old code would need to be modified. I do not think thats a good idea unless absolutely necessary.

@candiduslynx
Copy link
Contributor Author

If I understand correctly, this proposal will break previous implementations, certainly the tests have changed suggesting old code would need to be modified. I do not think thats a good idea unless absolutely necessary.

In this case it is just fixing the bug that exists in the mainline version.
It also allows for easier embedding of the cments from imported/censored structs.

@samlown as the test update suggests, the only issue seen here is adding the full pkg path for the import (or adding a root of vendored pkg to traverse correctly).

Could you please reassess this?

@candiduslynx
Copy link
Contributor Author

@samlown any updates?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs tests Not enough tests for this to be accepted
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants