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

[Fix] version detection: support processor virtual filename #2949

Merged
merged 1 commit into from Mar 24, 2021
Merged

[Fix] version detection: support processor virtual filename #2949

merged 1 commit into from Mar 24, 2021

Conversation

JounQin
Copy link
Contributor

@JounQin JounQin commented Mar 23, 2021

close #2948

cc @ljharb

lib/util/version.js Outdated Show resolved Hide resolved
tests/util/version.js Outdated Show resolved Hide resolved
tests/util/version.js Show resolved Hide resolved
@ljharb
Copy link
Member

ljharb commented Mar 23, 2021

What I've been playing with is:

diff --git a/lib/util/version.js b/lib/util/version.js
index c34b7c55..7f99dd96 100644
--- a/lib/util/version.js
+++ b/lib/util/version.js
@@ -7,6 +7,7 @@
 
 const resolve = require('resolve');
 const path = require('path');
+const fs = require('fs');
 const error = require('./error');
 
 let warnedForMissingVersion = false;
@@ -27,8 +28,20 @@ function detectReactVersion(context) {
     return cachedDetectedReactVersion;
   }
 
+  let basedir = process.cwd();
+  if (context) {
+    const filename = context.getFilename();
+    const fileDir = path.dirname(filename);
+    try {
+      const isFile = fs.statSync(filename).isFile();
+      const fileDirIsDir = fs.statSync(fileDir).isDirectory();
+      if (fileDirIsDir && isFile) {
+        basedir = fileDir;
+      }
+    } catch (e) { /**/ }
+  }
   try {
-    const reactPath = resolve.sync('react', {basedir: context ? path.dirname(context.getFilename()) : process.cwd()});
+    const reactPath = resolve.sync('react', {basedir});
     const react = require(reactPath); // eslint-disable-line global-require, import/no-dynamic-require
     cachedDetectedReactVersion = react.version;
     return cachedDetectedReactVersion;

@JounQin
Copy link
Contributor Author

JounQin commented Mar 23, 2021

There is a flow version test too, so an extra util function is preferred.


@ljharb You implementation will not try the true dirname anymore but use cwd as fallback, I don't think this is expected?

@ljharb
Copy link
Member

ljharb commented Mar 23, 2021

Indeed; my implementation isn't ideal, i just meant the overall approach of using fs methods.

@JounQin
Copy link
Contributor Author

JounQin commented Mar 23, 2021

@ljharb Done!

lib/util/version.js Outdated Show resolved Hide resolved
lib/util/version.js Outdated Show resolved Hide resolved
@JounQin
Copy link
Contributor Author

JounQin commented Mar 24, 2021

@ljharb Although I disagree with your opinion, but these changes are just simple, so Done!

@JounQin JounQin requested a review from ljharb March 24, 2021 02:14
Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

Thanks, this looks great

@ljharb ljharb changed the title fix: support processor virtual filename [Fix] version detection: support processor virtual filename … Mar 24, 2021
@ljharb ljharb changed the title [Fix] version detection: support processor virtual filename … [Fix] version detection: support processor virtual filename Mar 24, 2021
@ljharb ljharb merged commit bc37220 into jsx-eslint:master Mar 24, 2021
@JounQin JounQin deleted the fix/virtual_filename branch March 24, 2021 04:35
@JounQin
Copy link
Contributor Author

JounQin commented Mar 24, 2021

@ljharb Is there a schedule to release a patch version? I think it is a serious issue because the original version breaks.

@ljharb
Copy link
Member

ljharb commented Mar 24, 2021

I've just published v7.23.1.

@JounQin
Copy link
Contributor Author

JounQin commented Mar 24, 2021

Thanks, although the CHANGELOG seems incorrect.

image

@ljharb
Copy link
Member

ljharb commented Mar 24, 2021

That's quite correct. There's nothing unreleased at the moment.

@JounQin
Copy link
Contributor Author

JounQin commented Mar 24, 2021

@ljharb I mean [7.23.1], see #2950

@ljharb
Copy link
Member

ljharb commented Mar 24, 2021

ah yes, thanks, a copypaste error.

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

Successfully merging this pull request may close these issues.

lib/util/version.js:31:31 is not compatible with processors with virtual filename
2 participants