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

feat: add fs/resolve-parent-paths-by #2897

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from

Conversation

gururaj1512
Copy link

@gururaj1512 gururaj1512 commented Sep 12, 2024

Resolves #2567.

Description

What is the purpose of this pull request?
Adds JS implementation for @stdlib/fs/resolve-parent-paths-by/

This pull request:

Related Issues

Does this pull request have any related issues?

This pull request:

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

No.

Checklist

Please ensure the following tasks are completed before submitting this pull request.


@stdlib-js/reviewers

@gururaj1512 gururaj1512 changed the title resolve-parent-paths-by feat: add @stdlib/fs/resolve-parent-paths-by Sep 12, 2024
@kgryte kgryte added Feature Issue or pull request for adding a new feature. Needs Review A pull request which needs code review. Utilities Issue or pull request concerning general utilities. labels Sep 16, 2024
@gururaj1512
Copy link
Author

hey @kgryte, can you explain this linting error in README file at line 3.
Screenshot 2024-09-16 165059

@Snehil-Shah
Copy link
Member

@gururaj1512 is this when you try to commit?

@gururaj1512
Copy link
Author

@Snehil-Shah that error occurs when commiting and also while running eslint

@Snehil-Shah
Copy link
Member

@gururaj1512 Weird. I can't seem to reproduce the error. Did you follow the setup guide properly? (make install, make init). Maybe try clean installing and setting up again (after make clean).
Also, can you show the full error and what exactly are/were you trying to commit?

@gururaj1512
Copy link
Author

@gururaj1512 Weird. I can't seem to reproduce the error. Did you follow the setup guide properly? (make install, make init). Maybe try clean installing and setting up again (after make clean). Also, can you show the full error and what exactly are/were you trying to commit?

Yaa, I have tried make clean & make install
Lint Error
This error occurs while running command npx eslint path/to/directory/

@Snehil-Shah
Copy link
Member

@gururaj1512 Weird. I can't seem to reproduce the error. Did you follow the setup guide properly? (make install, make init). Maybe try clean installing and setting up again (after make clean). Also, can you show the full error and what exactly are/were you trying to commit?

Yaa, I have tried make clean & make install Lint Error This error occurs while running command npx eslint path/to/directory/

Ok, was able to reproduce it. Seems to me, it's something specific to npx as I am able to commit the file without any errors. And in any case, you don't need to run npx eslint manually, the linter is automatically triggered before every commit via git hooks (assuming your environment is set up as expected). If you still wish to manually trigger the linter, you can use the make recipes (like make lint) listed here.

Copy link
Member

@Snehil-Shah Snehil-Shah left a comment

Choose a reason for hiding this comment

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

LGTM otherwise!

Copy link
Member

@Snehil-Shah Snehil-Shah left a comment

Choose a reason for hiding this comment

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

Also, was the issue with your dev environment resolved @gururaj1512?

@gururaj1512
Copy link
Author

Also, was the issue with your dev environment resolved @gururaj1512?

@Snehil-Shah, the issue with my dev environment has been resolved. Thank you for checking in!

Copy link
Member

@Snehil-Shah Snehil-Shah left a comment

Choose a reason for hiding this comment

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

LGTM. On to @kgryte.

@kgryte kgryte changed the title feat: add @stdlib/fs/resolve-parent-paths-by feat: add fs/resolve-parent-paths-by Sep 24, 2024
Comment on lines 199 to 202
if ( bool ) {
test( spath, onTest );
}
FLG += 1;
Copy link
Member

Choose a reason for hiding this comment

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

This logic seems off. test may be an asynchronous function. In which case, the onTest callback will be called after hitting FLG += 1. If done multiple times, that could mean that L203-221 could be exercised before any onTest call is complete. This race condition needs to be addressed.

Copy link
Author

Choose a reason for hiding this comment

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

image
@kgryte, I wanted to clarify whether using async/await to wait for the test function to complete would be an appropriate solution here.

Copy link
Member

Choose a reason for hiding this comment

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

No, no using async/await. This implementation should be entirely callback-based

Copy link
Author

Choose a reason for hiding this comment

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

function some( paths, dir, test, done ) {
	var child;
	var spath;
	var FLG;
	var out;

	FLG = 0; // initialize flag to track if we are done traversing a directory level
	out = [];

	// Start at a base directory and continue moving up through each parent directory...
	return next( dir );

	/**
	* Resolves paths within a directory.
	*
	* @private
	* @param {string} dir - directory to search
	*/
	function next( dir ) {
		var i;
		for ( i = 0; i < paths.length; i++ ) {
			spath = resolve( dir, paths[ i ] );
			exists( spath, getCallback( spath ) );
		}
	}

	/**
	 * Returns a callback to be invoked upon checking for path existence.
	*
	* @private
	* @param {string} spath - resolved path
	* @returns {Callback} callback
	*/
	function getCallback( spath ) {
		return onExists;

		/**
		* Callback invoked after testing a resolved path.
		*
		* @private
		* @param {(Error|null)} error - error object
		* @param {boolean} bool - boolean indicating if a path exists
		* @returns {void}
		*/
		function onTest( error, bool ) {
			if ( error ) {
				return done( error );
			}
			if ( bool ) {
				out.push( spath );
			}
			// Increment flag after `test` completes:
			FLG += 1;
			if ( FLG === paths.length ) {
				// Check if we have resolved any paths...
				if ( out.length > 0 ) {
					return done( null, out );
				}
				// Resolve a parent directory:
				child = dir;
				dir = resolve( dir, '..' );

				// Reset flag:
				FLG = 0;

				// If we have already reached root, we cannot resolve any higher directories...
				if ( child === dir ) {
					return done( null, out );
				}
				// Resolve paths at next directory level:
				return next( dir );
			}
		}

		/**
		* Callback invoked after checking for path existence.
		*
		* @private
		* @param {(Error|null)} error - error object
		* @param {boolean} bool - boolean indicating if a path exists
		* @returns {void}
		*/
		function onExists( error, bool ) { // eslint-disable-line node/handle-callback-err
			if ( bool ) {
				// Pass the path to the `test` function and use `onTest` as the callback:
				test( spath, onTest );
			} else {
				// Increment flag if the path does not exist:
				FLG += 1;
				if ( FLG === paths.length ) {
					// Check if we have resolved any paths...
					if ( out.length > 0 ) {
						return done( null, out );
					}
					// Resolve a parent directory:
					child = dir;
					dir = resolve( dir, '..' );

					// Reset flag:
					FLG = 0;

					// If we have already reached root, we cannot resolve any higher directories...
					if ( child === dir ) {
						return done( null, out );
					}
					// Resolve paths at next directory level:
					return next( dir );
				}
			}
		}
	}
}

Does this work out

Copy link
Author

Choose a reason for hiding this comment

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

@kgryte, Am I doing it right in above mentioned programme, It passes all test cases & would pass the race conditions during async processing

*/
function onExists( error, bool ) { // eslint-disable-line node/handle-callback-err
if ( bool ) {
test( spath, onTest );
Copy link
Member

Choose a reason for hiding this comment

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

Same problem here.

@kgryte kgryte added Needs Changes Pull request which needs changes before being merged. and removed Needs Review A pull request which needs code review. labels Sep 24, 2024
Copy link
Member

@kgryte kgryte left a comment

Choose a reason for hiding this comment

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

This PR needs to address the race conditions during async processing before we can merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Issue or pull request for adding a new feature. Needs Changes Pull request which needs changes before being merged. Utilities Issue or pull request concerning general utilities.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RFC]: add @stdlib/fs/resolve-parent-paths-by
3 participants