Skip to content

///<reference path=... /> is case sensitive on Linux, but case insensitive on Windows #3626

Closed

Description

@duanyao

tsc treats path values in ///<reference path=... /> as case sensitive on linux, but case insensitive on Windows. This behavior makes codes writing on windows not portable if the casing of source file paths is inconsistant with the paths in ///<reference path=... />.

Activity

  1. kitsonk commented on Jun 25, 2015

    @kitsonk
    Contributor

    Isn't that an underlying OS challenge? I am not sure how you can resolve that cleanly, because there is no way to force either one at the runtime engine perspective to behave like the other. You can't force Linux to cover off every permutation of case sensitivity. The only option is to see if the actual file name returned from the OS call on Windows matches the case of the original file, and I wonder if that is actually worth the overhead. There are several issues including nodejs/node-v0.x-archive#6000 that discuss the issues/challenges with the support in NodeJS.

  2. duanyao commented on Jun 25, 2015

    @duanyao
    Author

    I think it is technically possible to make the behavior on Windows becomes case sensitive, because Windows file system is case-preserving, i.e. when you ask what the path of a file is, Windows gives you the result in correct case. So after tsc has resolved the referenced file, it can query the actual path from the OS, and compare it with the path specified in .ts case-sensitively; if not matching, fire an error.

    It seems that the node's issue you mentioned won't be fixed because of backward compatibility problem.

  3. mhegazy commented on Jun 25, 2015

    @mhegazy
    Contributor

    It should be fairly easy to add a compiler flag to always treat file lookups as case sensitive. the infrastructure is already in, it is just now surfaced as a compiler option. We would consider a PR if you are interested. you will need to do some pluming through in all places where getCanonicalFileName is used.

  4. duanyao commented on Jun 26, 2015

    @duanyao
    Author

    Mohamed Hegazy (@mhegazy) Good news, thank you. I'd like to look at this when I have time, though can't promise.

  5. benliddicott commented on Jul 16, 2015

    @benliddicott

    This seems like an issue for the build system rather than the language...

    Looks like an issue for a lint-like tool to me. I.e. warning on uppercase letters in filenames.

  6. duanyao commented on Jul 16, 2015

    @duanyao
    Author

    benliddicott tsc is the build system involved. Always using lowercase letters in filenames may be OK, but I think it is more natural to use the same name for a class and the file it lives in.

  7. benliddicott commented on Jul 17, 2015

    @benliddicott

    That could be two compiler warnings then:

    1. Warn if the filename in a ///<reference path=""/> comment differs in case from the filename on disk
    2. Warn if two files on disk are found with the same name differing only by case.

    They can only be warnings, because the first is not incorrect on case-insensitive systems, and the second is not incorrect on case-sensitive systems. Yet either is probably a mistake by somebody.

    I guess the first is most important as a major workflow is develop on Windows, deploy on Linux, and the second doesn't need to be part of the compiler at all (and might be easier as a standalone tool like node checkFilenameCase.js.

  8. added this to the milestone on Jul 17, 2015
  9. MartyIX commented on Oct 14, 2015

    @MartyIX
    Contributor

    Mohamed Hegazy (@mhegazy) Daniel Rosenwasser (@DanielRosenwasser) I was doing some research for this issue and I'm stuck because I don't know how to actually get "canonical path" of a file from NodeJs. Any suggestions are very welcomed.

    Problematic part of the code is this line: https://github.com/Microsoft/TypeScript/blob/master/src/compiler/sys.ts#L200 where you guys check if given file exists. And it exists for possible capitalizations of the file path.

    I went through NodeJs fs API and I haven't found any useful function for this (I have already posted a question to Node.js community - nodejs/node#3352 with a request for help.)

    Do you have any idea how to move forward?


    Apparently, it is not so simple to get canonical path in .NET either: http://stackoverflow.com/questions/4763117/how-can-i-obtain-the-case-sensitive-path-on-windows

    Update: This can do the trick http://stackoverflow.com/questions/33086985/how-to-obtain-case-exact-path-of-a-file-in-node-js-on-windows

  10. modified the milestones: TypeScript 1.8, on Oct 15, 2015
  11. 2 remaining items

  12. MartyIX commented on Oct 16, 2015

    @MartyIX
    Contributor

    I see, it looks great that one will be able to prevent some typos in file names without performance hit. :)


    I have tried the snippet from StackOverflow and it is nice that it allows you to write portable TypeScript scripts. My alpha patch has the feature under a compiler flag.

  13. duanyao commented on Oct 18, 2015

    @duanyao
    Author

    Hi Vladimir Matveev (@vladima) and MartyIX ,
    It seems not difficult to get case-exact path on Windows Vista+. Windows API GetFinalPathNameByHandle does this trick. On older Windows, use CreateFileMapping + MapViewOfFile + GetMappedFileName, though I believe the usage of Windows XP in TS community is neglectable now.

    Update: here is an example of getting case-exact path on Windows with node.js. You need to install package ffi to invoke Windows API.

    // exact-path.js
    var Kernel32 = new ffi.Library("Kernel32.dll", {
        "CreateFileW": [ "pointer", [ "pointer", "int", "int", "pointer", "int", "int", "pointer" ] ],
        "GetFinalPathNameByHandleW": [ "int", [ "pointer", "pointer", "int", "int" ] ],
        "CloseHandle": [ "int", [ "pointer"]],
    });
    var pathPrefix = '\\\\?\\';
    var pathBuf = new Buffer(2048);
    var encoding = "UCS-2";
    
    function getExcatPathWin(path) {
        var count = pathBuf.write(path, encoding);
        if (count > pathBuf.length - 2) throw new Error('too long path: ' + path);
        pathBuf[count] = 0;
        pathBuf[count + 1] = 0;
        var fh = Kernel32.CreateFileW(pathBuf, 0, 7 /* shareMode: all */, null, 3 /* OPEN_EXISTING */, 0, null);
        // we don't check error here, since GetFinalPathNameByHandleW will fail on invalid file handle anyway
        var count = Kernel32.GetFinalPathNameByHandleW(fh, pathBuf, pathBuf.length / 2, 0);
        Kernel32.CloseHandle(fh);
        if (count === 0) throw new Error('faild to CreateFile or GetFinalPathNameByHandle: ' + path);
        var exactPath = pathBuf.toString(encoding, 0, count * 2);
        if (exactPath.indexOf(pathPrefix) === 0) exactPath = exactPath.slice(pathPrefix.length);
        return exactPath;
    }
    
    console.log(getExcatPathWin(process.argv[2]));
  14. MartyIX commented on Oct 25, 2015

    @MartyIX
    Contributor

    Duan Yao (@duanyao) Please consider adding your solution to my question http://stackoverflow.com/questions/33086985/how-to-obtain-case-exact-path-of-a-file-in-node-js-on-windows. I'll gladly upvote. :-)

    I would love a flag to ensure that paths are correct on Windows and Linux, but it is not my decision to make.

  15. vladima commented on Oct 27, 2015

    @vladima
    Contributor

    fixed in e811fec

  16. mhegazy commented on Oct 27, 2015

    @mhegazy
    Contributor

    The fix should be in typescippt@next tonight. Use the new --forceConsistentCasingInFileNames command line argument to make sure you are catching casing issues consistently.

  17. leocaseiro commented on May 31, 2016

    @leocaseiro

    Hi Vladimir Matveev (@vladima), is there any option to use forceConsistentCasingInFileNames on .tsconfig file?
    I couldn't find it.
    Thanks

  18. mhegazy commented on May 31, 2016

    @mhegazy
    Contributor

    All compiler options can be specified in tsconfig. here is more details on them: http://www.typescriptlang.org/docs/handbook/compiler-options.html

  19. leocaseiro commented on Jun 1, 2016

    @leocaseiro

    thanks a lot

  20. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions