nodejs / nodejs/node

--preserve-symlinks-main not respected when using --require and --inspect-brk together

Đang mở
#44,856 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

inspector module
Ngôn ngữ chính
JavaScript
Star
122k
Fork
37.3k
Merge trung bình
4 ngày 2 giờ
Pull request đã merge (30 ngày)
283

Mô tả

Version

16.11.0

Platform

Darwin NAMTJ0390J2KQ 21.6.0 Darwin Kernel Version 21.6.0: Mon Aug 22 20:19:52 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T6000 arm64

Subsystem

internal/modules/cjs

What steps will reproduce the bug?

No response

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior?

That symlinks are preserved for main entrypoint file path resolution when --require, --preserve-symlinks-main, and --inspect-brk flags are all used.

What do you see instead?

When using a symlinked entry point, and calling node with all of --require, --preserve-symlinks-main, and --inspect-brk, the symlink for the entry point is not preserved, and the realpath of the entrypoint is used for module loading (and future module loads relative to that entry point).

Additional information

When using node --require preload.js --inspect-brk thing.js, Module.prototype._compile is entered for preload.js before the main entrypoint is loaded. Note the following code block:

https://github.com/nodejs/node/blob/main/lib/internal/modules/cjs/loader.js#L1126

 if (getOptionValue('--inspect-brk') && process._eval == null) {
    if (!resolvedArgv) {
      // We enter the repl if we're not given a filename argument.
      if (process.argv[1]) {
        try {
          resolvedArgv = Module._resolveFilename(process.argv[1], null, false);
        } catch {
          // We only expect this codepath to be reached in the case of a
          // preloaded module (it will fail earlier with the main entry)
          assert(ArrayIsArray(getOptionValue('--require')));
        }
      } else {
        resolvedArgv = 'repl';
      }
    }

Note especially that resolvedArgv = Module._resolveFilename(process.argv[1], null, false); will resolve the main entrypoint filename with the isMain arg set to false. This will cause --preserve-symlinks-main not to be applied for the filename resolution, and the realpath rather than the superficial (not following the links) path to be used.

Later, when the main entrypoint is loaded "for real", Module._resolveFilename is called with isMain = true (correctly). However, when this enters Module._findPath, the filename from the previous resolution, with isMain = false, is pulled from the cache on Module._pathCache, because the cache key does not incorporate the isMain arg value - only path info -, and this value is returned from Module._resolveFilename:

https://github.com/nodejs/node/blob/2a4452a53af65a13db4efae474162a7dcfd38dd5/lib/internal/modules/cjs/loader.js#L549

 const cacheKey = request + '\x00' + ArrayPrototypeJoin(paths, '\x00');
  const entry = Module._pathCache[cacheKey];
  if (entry)
    return entry;

So, even though a different file path would potentially emerge as a result of the logic below, the value from the cache is used:

https://github.com/nodejs/node/blob/2a4452a53af65a13db4efae474162a7dcfd38dd5/lib/internal/modules/cjs/loader.js#L580

 if (!isMain) {
          if (preserveSymlinks) {
            filename = path.resolve(basePath);
          } else {
            filename = toRealPath(basePath);
          }
        } else if (preserveSymlinksMain) {
          // For the main module, we use the preserveSymlinksMain flag instead
          // mainly for backward compatibility, as the preserveSymlinks flag
          // historically has not applied to the main module.  Most likely this
          // was intended to keep .bin/ binaries working, as following those
          // symlinks is usually required for the imports in the corresponding
          // files to resolve; that said, in some use cases following symlinks
          // causes bigger problems which is why the preserveSymlinksMain option
          // is needed.
          filename = path.resolve(basePath);
        } else {
          filename = toRealPath(basePath);
        }

Potential actions:

  1. Add isMain to the cache key used for Module._pathCache in Module._findPath. I think this is important, as other code in node that uses Module._findPath may create race conditions, because the cache key is insufficiently inclusive, making the function, in a sense, impure.
  2. Pass isMain = true to the Module._resolveFilename call in the --inspect-brk logic in Module.prototype._compile. Isn't it guaranteed that we are dealing with the main module here, anyway?

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu trong lib/internal/modules/cjs/loader.js, tập trung vào đường đi của --inspect-brk trong Module.prototype._compile, Module._resolveFilename và khóa Module._pathCache trong Module._findPath. Tái hiện bằng một entry point được liên kết bằng symlink cùng với --require, --preserve-symlinks-main và --inspect-brk, sau đó xác minh rằng entry point chính giữ nguyên đường dẫn symlink thay vì được phân giải thành đường dẫn thực.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
javascript, nodejs
Lĩnh vực
backend
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
48/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.