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

NEW: @W-15806448@ Implement core run behavior #15

Merged
merged 3 commits into from
May 23, 2024

Conversation

stephen-carter-at-sf
Copy link
Collaborator

I split this PR into 2 commits.

  • The first commit takes care of the validation of the client input to the run method and passing this off to each of the engines
  • The second commit takes care of validating and transforming the results from each engine into the overall RunResults

this.emitLogEvent(LogLevel.Debug, getMessage('RunningEngineWithRules', engineName, JSON.stringify(rulesToRun)));
const engine: engApi.Engine = this.getEngine(engineName);
const apiEngineRunResults: engApi.EngineRunResults = engine.runRules(rulesToRun, engineRunOptions);
validateEngineRunResults(engineName, apiEngineRunResults, ruleSelection);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Note that I currently have this implemented such it throws an error if something is wrong with what the engine returns. But we could if we wanted to, just emit a log event here with "Error" severity... and then have clients simply inform the user that something bad happened... but continue returning the results even if they are wrong.

But i'm leaning towards just erroring here so that users actually get upset and inform us if we have an engine giving back bad results. That is, I think I'd rather immediately fix an engine and deploy a fix asap (because it really shouldn't happen if we code up the engines well). But i'm open to change this if others disagree. It make sense to change it once we open up the engine api... but right now I propose we keep it as is.

@@ -0,0 +1,6 @@
import path from "node:path";

export function toAbsolutePath(fileOrFolder: string): string {
Copy link
Contributor

Choose a reason for hiding this comment

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

In the current scanner, we use a library called normalize-path for this. If it's really just a simple one-liner then we can do it ourselves, but if not we could consider continuing to delegate it.

Copy link
Collaborator Author

@stephen-carter-at-sf stephen-carter-at-sf May 23, 2024

Choose a reason for hiding this comment

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

As you've probably noticed... I'm trying my best to avoid bringing in unnecessary dependencies that we then need to maintain. Also, normalize-path (https://github.com/jonschlinkert/normalize-path/blob/master/index.js) actually does something different from what I'm doing here.

First, normalize-path always makes the path have forward slashes... which doesn't make sense for windows operating systems that have things like C:\Users\Some\Folder (causing the path to say something like C:/Users/Some/Folder which is invalid).

Second: what my code does is actually fix relative paths to use the correct slash according to the operating system in which it runs and then converts it to an absolute path if it isn't already one.

You'll notice that in some of my test examples I use different slashes to cover it fixing the slashes as needed and that I run my tests both on windows and linux to confirm that things are working.

@@ -117,4 +119,13 @@ export class RuleSelectionImpl implements RuleSelection {
getRulesFor(engineName: string): Rule[] {
return this.ruleMap.get(engineName) || [];
}

getRule(engineName: string, ruleName: string): Rule {
for (const rule of this.getRulesFor(engineName)) {
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 how it would happen, but would the existence of two rules with the same name be an error case as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah good question. I'll add in a validation step when we retrieve the rules from the engine via the describeRules method to ensure that they are unique.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added a commit to this PR to add in this validation.

packages/code-analyzer-core/src/results.ts Show resolved Hide resolved
@@ -131,4 +151,154 @@ function createEngineFromPlugin(enginePlugin: engApi.EnginePluginV1, engineName:
} catch (err) {
throw new Error(getMessage('PluginErrorFromCreateEngine', engineName, (err as Error).message), {cause: err})
}
}

function extractEngineRunOptions(runOptions: RunOptions): engApi.RunOptions {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd like to see some documentation for this method and the other validation methods. At the very least, a JSDoc header would go a long way.

Copy link
Collaborator Author

@stephen-carter-at-sf stephen-carter-at-sf May 23, 2024

Choose a reason for hiding this comment

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

I have scheduled to add in doc comments soon as a separate polishing work item. I mentioned this in one of my earlier reviews... but i'm waiting for things to stabilize and finalize before doing so to avoid having to maintain things while things are still changing a bit.

@stephen-carter-at-sf stephen-carter-at-sf merged commit 45b3708 into dev May 23, 2024
5 checks passed
@stephen-carter-at-sf stephen-carter-at-sf deleted the d/W-15806448 branch May 23, 2024 14:38
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