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

Display the file paths in the error message for the error "the command .myCommand() is already defined." #4313

Open
MurzNN opened this issue Nov 27, 2024 · 4 comments · May be fixed by #4324

Comments

@MurzNN
Copy link

MurzNN commented Nov 27, 2024

Description

When we get the error "the command .myCommand() is already defined." it's not always easy to understand in which exact files we got this duplication of commands.

So it would be great to display paths of both files that lead to this error.

Suggested solution

Add the file paths to the error message output, something like this:
File lib/api/_loaders/_command-loader.js - function validateMethod():

-       const err = new TypeError(`Error while loading the API commands: the ${this.type} ${this.namespace || ''}.${this.commandName}() is already defined.`);
+      const err = new TypeError(`Error while loading the API commands from the file ${this.fileName}: the ${this.type} ${this.namespace || ''}.${this.commandName}() is already defined by the file [the first file path].`);

But I can't find a way to find the first file path. When I find, I can make a PR with this feature.

Alternatives / Workarounds

No response

Additional Information

No response

@garg3133
Copy link
Member

@MurzNN myCommand is usually the same as the file name of the command, is it not happening in your case? Or do you want the complete path of the file from the this.fileName?

@MurzNN
Copy link
Author

MurzNN commented Nov 28, 2024

Yeah, I want to see the complete path, because in some projects we have the same named commands in dependencies, and sometimes - symlinks to the same Nigtwatch directory from different places, which leads to this error, with no idea about what exact directories lead to this problem.

@ritankarsaha
Copy link

ritankarsaha commented Dec 5, 2024

@garg3133 @MurzNN

To address this issue we can modify the validateMethod function in Nightwatch's command loader to track and display the file paths where duplicate commands are defined. We can add a command registry and maintain a global registry to keep track of commands and their file paths during loading. This registry can map each command to the file path where it was first defined and then check if a command already exists in the registry. If a duplicate is found, we can throw an error that includes the file paths of both the first definition and the duplicate.
We can Include the file paths in the error message to clearly show where the duplication occurred.

I think this is the solution to this .... Is the implementation to this correct?

@STAR-173
Copy link

STAR-173 commented Dec 8, 2024

Hey @garg3133 ,
I have implemented the displaying of file names and also written a unit test.
Can you can take a look into the PR

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

Successfully merging a pull request may close this issue.

4 participants