Repository: parcel-bundler/parcel
File: packages/resolvers/glob/src/GlobResolver.js (JavaScript)

Code before:
    }

    let invalidateOnFileCreate = [];
    let invalidateOnFileChange = new Set();

    switch (specifier[0]) {
      // Path specifier
      case '.': {
        specifier = path.resolve(path.dirname(sourceFile), specifier);
        break;
      }

      // Absolute path. Make the glob relative to the project root.
      case '/': {
        specifier = path.resolve(options.projectRoot, specifier.slice(1));
        break;
      }

      // Tilde path. Package relative. Resolve relative to nearest node_modules
      // directory, the nearest directory with package.json or the project
      // root - whichever comes first.
      case '~': {
        const insideNodeModules = options.projectRoot.includes('node_modules');
        let dir = path.dirname(sourceFile);

        while (
          dir !== options.projectRoot &&
          path.basename(path.dirname(dir)) !== 'node_modules' &&
          (insideNodeModules ||
            !(await options.inputFS.exists(path.join(dir, 'package.json'))))
        ) {
          dir = path.dirname(dir);

          if (dir === path.dirname(dir)) {
            dir = options.projectRoot;
            break;
          }
        }

        specifier = path.resolve(dir, specifier.slice(2));
        break;
      }

      // Support package-ish specifiers like:
      //   foo      (node_module)
      //   @foo/bar (scoped node_module)
      //
      // First we resolve the initial portion using NodeResolver, then we tack
      // on the remaining glob.
      default: {
        // Globs are not paths - so they always use / (see https://github.com/micromatch/micromatch#backslashes)

Review comment (suggestion):
`fs.findAnchestorFile(["package.json"], dir, options.projectRoot)` does exactly this

https://github.com/parcel-bundler/parcel/blob/707e5295f6b1874419c2c3d3aa6ec7f10d6a7c81/packages/core/package-manager/src/NodePackageManager.js#L554-L558

Code after:
    }

    let invalidateOnFileCreate = [];
    let invalidateOnFileChange = new Set();

    switch (specifier[0]) {
      // Path specifier
      case '.': {
        specifier = path.resolve(path.dirname(sourceFile), specifier);
        break;
      }

      // Absolute path. Make the glob relative to the project root.
      case '/': {
        specifier = path.resolve(options.projectRoot, specifier.slice(1));
        break;
      }

      // Tilde path. Package relative. Resolve relative to nearest node_modules
      // directory, the nearest directory with package.json or the project
      // root - whichever comes first.
      case '~': {
        let dir = path.dirname(sourceFile);
        let pkgPath = nullthrows(
          options.inputFS.findAncestorFile(
            ['package.json'],
            dir,
            options.projectRoot,
          ),
        );
        specifier = path.resolve(path.dirname(pkgPath), specifier.slice(2));
        break;
      }

      // Support package-ish specifiers like:
      //   foo      (node_module)
      //   @foo/bar (scoped node_module)
      //
      // First we resolve the initial portion using NodeResolver, then we tack
      // on the remaining glob.
      default: {
        // Globs are not paths - so they always use / (see https://github.com/micromatch/micromatch#backslashes)
        let splitOn = specifier.indexOf('/');
        if (specifier[0] === '@') {
          splitOn = specifier.indexOf('/', splitOn + 1);
        }

        // Since we've already asserted earlier that there is a glob present, it shouldn't be
        // possible for there to be only a package here without any other path parts (e.g. `import('pkg')`)
        invariant(splitOn !== -1);