flow icon indicating copy to clipboard operation
flow copied to clipboard

flow v0.57.x don't shows no errors if use `module.system.node.resolve_dirname=src`

Open gin0606 opened this issue 9 years ago • 29 comments

I need help!

flow don't show any errors, if write module.system.node.resolve_dirname=src in .flowconfig

https://github.com/gin0606/flow-v0.57.1-trouble

Please see this repository, that can see my trouble.

Thanks!

Maybe related to https://github.com/flowtype/flow-bin/issues/91 and https://github.com/facebook/flow/issues/5105

gin0606 avatar Oct 25 '17 04:10 gin0606

Still an issue with 0.58.0

apsavin avatar Nov 03 '17 14:11 apsavin

I'd encountered the same problem, and dug down into a code of the Flow, then I think that I found what the reason it caused.

First, in version 0.57 of Flow, the feature as described below added

New Features: Flow will now only check the files in node_modules/ which are direct or transitive dependencies of the non-node_modules code.

Also, the document explains about setting module.system.node.resolve_dirname as below.

module.system.node.resolve_dirname (string) By default, Flow will look in directories named node_modules for node modules. You can configure this behavior with this option.

In other words, adding a setting to resolve_dirname means that a directory added will not be checked as well as node_modules unless imported from the file where not in the directory set to resolve_dirname and not set to the ignore option.

As far as the document explained, I think this behavior may be the intended of the Flow team. But it will confuse some users, including me.

My workaround is to import files where Flow check lazily, from files where Flow check immediately

For example, in case of @gin0606 https://github.com/gin0606/flow-v0.57.1-trouble, we will be able to force type checking by placing the file like below in the root directory of the repository.

// ./src/index.js
// @flow
import bar from 'foo/bar';

let num: number = 1;
let numerror: number = 'string';
let string: string = bar();

console.log(num);
console.log(numerror);
console.log(string);
+ 
+ export default () => {};

// In root of repository ./root.js
+ import fn from "./src"

But the workaround seems not absolutely ideally. My personal opinion is that it should be checked lazily node_modules directory only. Because some users use resolve_dirname option conjunction with resolve.modules option of the Webpack.

If we need PR to make such corrections, I will be honored to make it.

However, for us to find an ideal solution about this, I would like to hear the opinion of the Flow team on this Issue especially @gabelevi 's one (because he added such feature in https://github.com/facebook/flow/commit/385da8e95b3d7e759f8419b8b86216a9aa9f8f42).

kogai avatar Nov 04 '17 10:11 kogai

I actually implemented a workaround for this problem. It doesn't keep the exact same behavior but you get your errors back in the case described by the OP:

Simply change:

module.system.node.resolve_dirname=src

to

module.system.node.resolve_dirname=./src

It will not give you the bubble effect but it will let you use your base directory for all imports

image

bradennapier avatar Nov 04 '17 11:11 bradennapier

Thanks @kogai !

For our project we've just replaced module.system.node.resolve_dirname=src with several module.name_mapper, like this:

module.name_mapper='^desktop/\(.*\)$' -> '<PROJECT_ROOT>/src/desktop/\1'

And it seems that it fixed the issue.

apsavin avatar Nov 04 '17 12:11 apsavin

On the project I am using now the relative url definitely fixes the issue.

bradennapier avatar Nov 04 '17 12:11 bradennapier

I'd like to echo the question from @kogai.

We are using Flow in a fairly large project, with the module.system.node.resolve_dirname rule pointing to our local source code.

Maintaining a list of individual name_mapper rules is going to be quite unwieldy and error prone. Converting every import statement to a relative import will also be difficult at this stage, and seems likely to increase the fragility of our codebase.

It would be great to hear the flow team's thoughts on this issue, and investigate possible alternatives.

ajhyndman avatar Nov 13 '17 01:11 ajhyndman

Experiencing the same issue. But here is the weird thing: on OSX it works perfectly fine. On our CI (Travis, Ubuntu 14.04.5 LTS) it fails to output any errors. Unfortunately the workaround of a list of name_mapper rules doesn't work. Maybe a difference with others is that we run flow check with the --all flag.

qamil avatar Nov 14 '17 11:11 qamil

@qamil I had the same issue just yesterday, and although I know that it probably won't help everyone, I made CI checking work by changing module.system.node.resolve_dirname=src to module.system.node.resolve_dirname=./src

stasgavrylov avatar Nov 14 '17 11:11 stasgavrylov

Any idea why the ./ trick works? Thanks to @bradennapier I've now got this:

[options]
module.system.node.resolve_dirname=node_modules
module.system.node.resolve_dirname=./app

...and it works great – Flow skips checking non-depended files within node_modules, but it always checks everything in app, which is what I want.

But I'm concerned this might be an unsupported hack, that it might get 'fixed' in a future Flow version and stop working. Is the ./ trick an approved way to opt-out of the 0.57.0 new feature, or is there a better method that is sanctioned by the Flow team?

callumlocke avatar Feb 19 '18 11:02 callumlocke

I think you can find the answer here: https://github.com/facebook/flow/issues/5647

alexeyraspopov avatar Feb 19 '18 12:02 alexeyraspopov

@callumlocke It seems like it works for me as well (with Flow 0.66.0), thanks!

JakubRimal avatar Feb 19 '18 12:02 JakubRimal

@alexeyraspopov I can't see an answer to my question there. Can you be more specific?

callumlocke avatar Feb 19 '18 14:02 callumlocke

@callumlocke, do not use resolve_dirname for your code. Use name_mapper instead.

alexeyraspopov avatar Feb 20 '18 09:02 alexeyraspopov

@alexeyraspopov can you point to where a Flow team member has said this is the better course of action? Or some official docs saying this?

My app has a lot of subdirs. Using several name_mapper lines (one for each subdir under app) is more complex, and a bit harder to maintain (need to remember to update the flow config whenever our directory structure changes). So before doing that, I want something official.

callumlocke avatar Feb 20 '18 10:02 callumlocke

@alexeyraspopov you are right, usage of module.name_mapper is the correct solution. It's a pity that it is not clear from the official documentation especially when module.system.node.resolve_dirname was commonly used for aliases.

@callumlocke for every folder under src add the following line to [options] section of your .flowconfig file:

module.name_mapper='^my_folder/\(.*\)$' -> '<PROJECT_ROOT>/src/my_folder/\1'

BTW now I think this issue can be closed... :)

JakubRimal avatar Feb 20 '18 10:02 JakubRimal

can you point to where a Flow team member has said this is the better course of action? Or some official docs saying this?

This is why my issue exists. Docs need improvements to emphasize this part.

alexeyraspopov avatar Feb 20 '18 10:02 alexeyraspopov

@callumlocke I agree that it is harder to maintain but the official documentation (https://flow.org/en/docs/config/options/#toc-module-system-node-resolve-dirname-string) says that module.system.node.resolve_dirname should be used for specifying of node modules location. Using it for aliases of our modules was kind of hack I think.

JakubRimal avatar Feb 20 '18 10:02 JakubRimal

@JakubRimal @alexeyraspopov OK, I have PR'd a docs update. Please go and upvote it if you agree: https://github.com/facebook/flow/pull/5850

callumlocke avatar Feb 20 '18 12:02 callumlocke

This issue is also being tracked at https://github.com/yahoo/react-intl/issues/1086

callumlocke avatar Feb 21 '18 14:02 callumlocke

module.system.node.resolve_dirname should be used for specifying of node modules location. Using it for aliases of our modules was kind of hack I think.

Right, it's bad that this hack is the only sane way to make flow work as expected right now.

dgcoffman avatar Mar 05 '18 21:03 dgcoffman