Flow: Should not get “Required module not found” error when importing a file ignored within .flowconfig

Created on 15 May 2017  Â·  4Comments  Â·  Source: facebook/flow

My project’s .flowconfig ignores a few folders, including core/bundles/:

~~~ini
[ignore]
/core/(bin\|bundles\|transpilation)/.*

other entries


~~~

I am ignoring these files because because they will never have types in them, and I think ignoring them will increase the speed of Flow’s type-checking by telling Flow that it doesn’t need to watch them.

I recently added an import within a Flow-typed file of a file within this ignored directory:

~~~javascript
// @flow

import {LRUMap} from '../../../../bundles/lru_map';
// the path resolves to core/bundles/lru_map

// 
 some code that uses LRUMap 

~~~

I am sure that the relative path '../../../../bundles/lru_map' is correct. The file can run and use LRUMap without errors, and the ESLint rule import/no-unresolved doesn’t give any warnings about that path, while it does if I remove or add any ../.

Current behavior

Flow gives me a “Required module not found” error:

~~~
core/tests/scripts/utils/caching/TestLRUMap.js:5
5: import {LRUMap} from '../../../../bundles/lru_map';
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ../../../../bundles/lru_map. Required module not found

Found 1 error
~~~

This is confusing because there really is a module at that path. It was hard to figure out why Flow couldn’t find it.

Expected behavior

I expect one of two things to happen instead of Flow showing this error:

  1. Flow should report no error for this line, because it can see that the file does exist. Of course, since the imported file has no type annotations, it won’t put any type constraints on how I use LRUMap, but I’m okay with that.

    This solution seems ideal to me; I don’t see any reason I shouldn’t be able to tell Flow to not watch a file while also importing it within Flow-typed code. And the docs for .flowconfig [ignore] say this is to determine “which files to read and watch for changes”, not to list files that Flow should not be able to see at all.

  2. Flow could report a more specific error for this case with a less confusing message. The message could be something like “Can’t import module that is configured to be ignored”. Ideally the message would also explain why this is the case and what to do instead, either in the message or by linking to a documentation page.

Workaround

Currently my workaround is to add a $FlowFixMe suppress_comment above the import line:

~javascript
// $FlowFixMe
import {LRUMap} from '../../../../bundles/lru_map';
~

Related issues

The authors of these issues and questions encountered this problem too:

Most helpful comment

@nmn Are you talking about module.name_mapper? I think I see how I could use it as an alternative workaround, by writing UntypedModule.js that contains let value: any; export default value;, and then using module.name_mapper to rewrite all imports within my bundles/ to that same file. Thanks for pointing that out.

But it's still a workaround, and Flow should improve what it does in situations like this. At the very least, the error message shouldn't imply that the imported module doesn't exist (as I described in option 2).

All 4 comments

You should be using module_mapper in addition and giving all your ignored modules the any type.

@nmn Are you talking about module.name_mapper? I think I see how I could use it as an alternative workaround, by writing UntypedModule.js that contains let value: any; export default value;, and then using module.name_mapper to rewrite all imports within my bundles/ to that same file. Thanks for pointing that out.

But it's still a workaround, and Flow should improve what it does in situations like this. At the very least, the error message shouldn't imply that the imported module doesn't exist (as I described in option 2).

FWIW, this can also happen if you forget to include a sibling or parent folder in the [include] section of your config, and try to import something from outside of your project root.

There is [untyped] section which takes list of regexp similar to [ignore] but treat those modules as any.

This probably can be closed.
/cc @vkurchatkin

Was this page helpful?
0 / 5 - 0 ratings

Related issues

john-gold picture john-gold  Â·  3Comments

cubika picture cubika  Â·  3Comments

ctrlplusb picture ctrlplusb  Â·  3Comments

Beingbook picture Beingbook  Â·  3Comments

tp picture tp  Â·  3Comments