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

Add On Import Translation of Python Files in Include Path #1964

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from

Conversation

konradweiss
Copy link
Collaborator

The PR adds a preprocessing function to all languages that is supposed to return files located in the includePaths that are supposed to be analyzed under their own component.

The current implementation for python identifies which file in the import paths should be used to fulfill which import statement and then recursively resolves additional imports in that file.

@konradweiss konradweiss changed the title Add on use inclusion of python files in Include Path Add On Import Translation of Python Files in Include Path Jan 23, 2025
Copy link
Contributor

@maximiliankaul maximiliankaul left a comment

Choose a reason for hiding this comment

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

I think there is some discussion needed before merging this into main. See internal chat + comments here.

@@ -140,15 +142,24 @@ private constructor(
result: TranslationResult,
): Set<LanguageFrontend<*, *>> {
val usedFrontends = mutableSetOf<LanguageFrontend<*, *>>()

val externalSources: MutableList<File> = mutableListOf()
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we please have some doc here? What are these variables used for?

return usedFrontends
}

private fun extractConfiguredSources(path: Path): MutableList<File> {
val rootFile = path.toFile()
return if (rootFile.exists())
Copy link
Contributor

Choose a reason for hiding this comment

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

Please refactor this code. Add some { as a start.

}
.toMutableList()

val importRe = "(?m)^(?:from[ ]+(\\S+)[ ]+)?import[ ]+(\\S+)[ ]*\$".toRegex()
Copy link
Contributor

Choose a reason for hiding this comment

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

I object having this regex here.

  • It's not documented.
  • I don't like parsing the sources manually with a regex -> can't we use the ast.parse result for this?
  • Can we please name groups, if we have to use a regex? This would make it much easier to understand what's going on.
  • What about import foo without the from part?
  • What about as in the import?

importRe.findAll(source.readText()).forEach { imp ->

// Only try to find file containing imports if we did not process this import so far
if (!processedImports.contains(imp.groupValues.get(0))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this approach works. An from foo ... import can refer to different imports, depending on the location of the file you're parsing here.

Copy link

codecov bot commented Jan 24, 2025

Codecov Report

Attention: Patch coverage is 55.83333% with 53 lines in your changes missing coverage. Please review.

Project coverage is 77.69%. Comparing base (b3d5d07) to head (bb9527f).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...sec/cpg/frontends/python/PythonLanguageFrontend.kt 52.85% 27 Missing and 6 partials ⚠️
...tlin/de/fraunhofer/aisec/cpg/TranslationManager.kt 52.50% 13 Missing and 6 partials ⚠️
...fraunhofer/aisec/cpg/frontends/LanguageFrontend.kt 0.00% 1 Missing ⚠️

❌ Your patch check has failed because the patch coverage (55.83%) is below the target coverage (75.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
Files with missing lines Coverage Δ
.../kotlin/de/fraunhofer/aisec/cpg/graph/Component.kt 90.90% <ø> (ø)
.../de/fraunhofer/aisec/cpg/frontends/TestLanguage.kt 83.33% <100.00%> (+0.47%) ⬆️
...fer/aisec/cpg/frontends/cxx/CXXLanguageFrontend.kt 77.77% <100.00%> (+0.06%) ⬆️
...r/aisec/cpg/frontends/golang/GoLanguageFrontend.kt 92.59% <100.00%> (+0.03%) ⬆️
...unhofer/aisec/cpg/frontends/ini/IniFileFrontend.kt 77.77% <100.00%> (+0.50%) ⬆️
...r/aisec/cpg/frontends/java/JavaLanguageFrontend.kt 71.18% <100.00%> (+0.54%) ⬆️
...fer/aisec/cpg/frontends/jvm/JVMLanguageFrontend.kt 86.56% <100.00%> (+0.20%) ⬆️
...aisec/cpg/frontends/llvm/LLVMIRLanguageFrontend.kt 88.34% <100.00%> (+0.11%) ⬆️
...r/aisec/cpg/frontends/ruby/RubyLanguageFrontend.kt 83.87% <100.00%> (+0.53%) ⬆️
...frontends/typescript/TypeScriptLanguageFrontend.kt 83.83% <100.00%> (+0.16%) ⬆️
... and 3 more

... and 2 files with indirect coverage changes

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

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