You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
///<reference path=... /> is case sensitive on Linux, but case insensitive on Windows #3626
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=... />.
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.
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.
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.
benliddicotttsc 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.
Warn if the filename in a ///<reference path=""/> comment differs in case from the filename on disk
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.
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.)
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.
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.jsvarKernel32=newffi.Library("Kernel32.dll",{"CreateFileW": ["pointer",["pointer","int","int","pointer","int","int","pointer"]],"GetFinalPathNameByHandleW": ["int",["pointer","pointer","int","int"]],"CloseHandle": ["int",["pointer"]],});varpathPrefix='\\\\?\\';varpathBuf=newBuffer(2048);varencoding="UCS-2";functiongetExcatPathWin(path){varcount=pathBuf.write(path,encoding);if(count>pathBuf.length-2)thrownewError('too long path: '+path);pathBuf[count]=0;pathBuf[count+1]=0;varfh=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 anywayvarcount=Kernel32.GetFinalPathNameByHandleW(fh,pathBuf,pathBuf.length/2,0);Kernel32.CloseHandle(fh);if(count===0)thrownewError('faild to CreateFile or GetFinalPathNameByHandle: '+path);varexactPath=pathBuf.toString(encoding,0,count*2);if(exactPath.indexOf(pathPrefix)===0)exactPath=exactPath.slice(pathPrefix.length);returnexactPath;}console.log(getExcatPathWin(process.argv[2]));
The fix should be in typescippt@next tonight. Use the new --forceConsistentCasingInFileNames command line argument to make sure you are catching casing issues consistently.
Activity
kitsonk commentedon Jun 25, 2015
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.
duanyao commentedon Jun 25, 2015
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.
mhegazy commentedon Jun 25, 2015
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.
duanyao commentedon Jun 26, 2015
Mohamed Hegazy (@mhegazy) Good news, thank you. I'd like to look at this when I have time, though can't promise.
benliddicott commentedon Jul 16, 2015
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.
duanyao commentedon Jul 16, 2015
benliddicott
tscis 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.benliddicott commentedon Jul 17, 2015
That could be two compiler warnings then:
///<reference path=""/>comment differs in case from the filename on diskThey 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.MartyIX commentedon Oct 14, 2015
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
fsAPI 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
2 remaining items
MartyIX commentedon Oct 16, 2015
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.
duanyao commentedon Oct 18, 2015
Hi Vladimir Matveev (@vladima) and MartyIX ,
It seems not difficult to get case-exact path on Windows Vista+. Windows API
GetFinalPathNameByHandledoes this trick. On older Windows, useCreateFileMapping + 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
ffito invoke Windows API.MartyIX commentedon Oct 25, 2015
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.
vladima commentedon Oct 27, 2015
fixed in e811fec
mhegazy commentedon Oct 27, 2015
The fix should be in
typescippt@nexttonight. Use the new--forceConsistentCasingInFileNamescommand line argument to make sure you are catching casing issues consistently.leocaseiro commentedon May 31, 2016
Hi Vladimir Matveev (@vladima), is there any option to use forceConsistentCasingInFileNames on
.tsconfigfile?I couldn't find it.
Thanks
mhegazy commentedon May 31, 2016
All compiler options can be specified in tsconfig. here is more details on them: http://www.typescriptlang.org/docs/handbook/compiler-options.html
leocaseiro commentedon Jun 1, 2016
thanks a lot