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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(o3r): replaced path.resolve with path.join for better path resolution #1782

Merged

Conversation

gavadhany
Copy link
Contributor

Proposed change

Change path resolver from path.resolve to path.join as the tree.exists is unable to resolve the absolute path generated by path.resolve

Related issues

@gavadhany gavadhany requested a review from a team as a code owner May 14, 2024 11:02
@@ -46,10 +46,10 @@ function ngGenerateServiceFn(options: NgGenerateServiceSchematicsSchema): Rule {
let packageName = destination;
if (workspaceProject?.projectType !== 'application') {
let rec = '..';
while (!tree.exists(path.resolve(destination, rec, 'package.json')) && path.resolve(destination, rec) !== '/') {
while (!tree.exists(path.join(destination, rec, 'package.json')) && path.resolve(destination, rec) !== '/') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
while (!tree.exists(path.join(destination, rec, 'package.json')) && path.resolve(destination, rec) !== '/') {
while (!tree.exists(path.join(destination, rec, 'package.json')) && path.join(destination, rec) !== '/') {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@matthieu-crouzet can't use "path.join" while comparing with root path as we need absolute path. Path.join returns relative path. Hence using path.resolve

Copy link
Contributor

Choose a reason for hiding this comment

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

But you are comparing with the tree path that is a relative path to the project
on Windows you will never have a path matching '/'
We don't want to resolve a path outside the project

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool.. will take a look prevent it from resolving paths outside projects

@gavadhany gavadhany force-pushed the bugfix/service-path-resolver branch from f97502a to 771e53a Compare May 15, 2024 06:00
kpanot
kpanot previously approved these changes May 15, 2024
@gavadhany
Copy link
Contributor Author

@matthieu-crouzet and @kpanot : I have refactored the code a bit. The current version of the code wouldnt work if package.json was in the path entered by user e.g. './'.
Also added a check to prevent path resolution outside the project workspace and an error package.json doesnt exist within the project workspace.

currentDirectory = path.join(currentDirectory, parent);
}
if (currentDirectory === parent) {
throw new SchematicsException('Could not find package.json in the project directory.');
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
throw new SchematicsException('Could not find package.json in the project directory.');
throw new O3rCliError('Could not find package.json in the project directory.');

from @o3r/schematics

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

let rec = '..';
while (!tree.exists(path.resolve(destination, rec, 'package.json')) && path.resolve(destination, rec) !== '/') {
rec = path.join('..', rec);
let currentDirectory = path.join(destination);
Copy link
Contributor

Choose a reason for hiding this comment

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

what is the purpose of path.join here? Maybe you wanted to used path.normalize?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea.. wanted to normalize. Replaced it with normalize

while (!tree.exists(path.resolve(destination, rec, 'package.json')) && path.resolve(destination, rec) !== '/') {
rec = path.join('..', rec);
let currentDirectory = path.join(destination);
let parent = '..';
Copy link
Contributor

Choose a reason for hiding this comment

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

the value never change, it should be const.
(I am surprised it has not been caught by the linter :S)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

let currentDirectory = path.join(destination);
let parent = '..';
// Find the package.json file in the project directory
while (currentDirectory !== parent && !tree.exists(path.join(currentDirectory, 'package.json'))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

did you test that currentDirectory !== parent is correctly executing in the different OS?
It seems to be quite weak condition, maybe a .include(parent) would be a bit stronger?

Copy link
Contributor Author

@gavadhany gavadhany May 15, 2024

Choose a reason for hiding this comment

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

I haven't tested on different OSs. But did run with path.posix implementation to verify and compare the results.

I can change it to includes if its stronger

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kpanot : Changed it from === to includes

@gavadhany gavadhany force-pushed the bugfix/service-path-resolver branch 3 times, most recently from b07a13a to 5ee911b Compare May 16, 2024 07:19
@gavadhany gavadhany force-pushed the bugfix/service-path-resolver branch from 5ee911b to 869eb9f Compare May 17, 2024 04:29
@kpanot kpanot force-pushed the bugfix/service-path-resolver branch from 869eb9f to 3dfaf78 Compare May 17, 2024 09:13
@kpanot kpanot merged commit b96eb00 into AmadeusITGroup:release/10.2 May 17, 2024
25 checks passed
@gavadhany gavadhany deleted the bugfix/service-path-resolver branch May 23, 2024 05:05
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.

None yet

4 participants