cordova-android icon indicating copy to clipboard operation
cordova-android copied to clipboard

getASPath.bat fails for project paths with parenthesis

Open Lindsay-Needs-Sleep opened this issue 6 years ago • 4 comments

Bug Report

Suddenly I couldn't build my project because it was coming at me with this error:

Could not find an installed version of Gradle either in Android Studio,
or on your system to install the gradle wrapper. Please include gradle
in your path, or install Android Studio

After a bit of digging my guess was because I had recently renamed my project folder to have parenthesis. (eg. "myCordova(chromecast)") Removing the parenthesis made it work again.

Problem

I believe the problem lies in myCordova(chromecast)\platforms\android\cordova\lib\getASPath.bat` (getASPath.bat on github)

When I uncomment lines 93 and 94 from myCordova(chromecast)\platforms\android\cordova\lib\check_reqs.js

92        var result = child_process.spawnSync(path.join(__dirname, 'getASPath.bat'));
93        console.log('result.stdout =' + result.stdout.toString());
94        console.log('result.stderr =' + result.stderr.toString());

I get this output:

result.stdout =
result.stderr ='S:\Projects\myCordova' is not recognized as an internal or external command,

So it looks like getASPath.bat does not handle the parenthesis well (since it truncated the path in the output). (See my next comment for more detailed debugging)

Command or Code

cordova run android

Environment, Platform, Device

Windows, Android

Version information

https://github.com/apache/cordova-android.git#4cf3dcfaae6dc82ddb1ccf439d209cbcc2f474a0 (getASPath.bat is the same on this commit as it is on master.)

Checklist

  • [x] I searched for existing GitHub issues
  • [x] I updated all Cordova tooling to most recent version
  • [x] I included all the necessary information above

Lindsay-Needs-Sleep avatar Aug 30 '19 16:08 Lindsay-Needs-Sleep

I don't believe the problem is in getASPath.bat, the error that stated is it cannot find S:\Projects\myCordova. As you said, it truncated & dropped everything after and including the special character.

Therefore it is a problem in calling getASPath.bat, instead of a problem executing it. I think replacing path.join with path.resolve will solve this issue because I'm pretty sure path.resolve will escape the special characters for you, but I don't have a windows machine to test.

If it fails, then we will need to find a way to escape special characters in file paths.

breautek avatar Sep 08 '19 23:09 breautek

I have added additional console logs. Here are the results:

console.log(__dirname);
// S:\Projects\myCordova(chromecast)\platforms\android\cordova\lib
console.log(path.join(__dirname, 'getASPath.bat'));
// S:\Projects\myCordova(chromecast)\platforms\android\cordova\lib\getASPath.bat

I believe it is safe to say the path is being formed correctly.

I believe the issue lies in windows parsing of the string.
To confirm this, in a cmd prompt I entered:

> S:\Projects\myCordova(chromecast)\platforms\android\cordova\lib\getASPath.bat

// The error I received:
chromecast : The term 'chromecast' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct 
and try again.
At line:1 char:23
+ S:\Projects\myCordova(chromecast)\platforms\android\cordova\lib\getAS ...
+                       ~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (chromecast:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

As suggested, I also tried

child_process.spawnSync(path.resolve(path.join(__dirname, 'getASPath.bat')));
// result.stderr ='S:\Projects\myCordova' is not recognized as an internal or external command,

I also tried:

`child_process.spawnSync('cmd.exe', ['/c', path.join(__dirname, 'getASPath.bat')]);`
// result.stderr ='S:\Projects\myCordova' is not recognized as an internal or external command,

`child_process.spawnSync('cmd.exe', ['/c', '"' + path.join(__dirname, 'getASPath.bat') + '"']);`
// result.stderr ='\"S:\Projects\myCordova(chromecast)\platforms\android\cordova\lib\getASPath.bat\"' is not recognized as an internal or external command,

Lindsay-Needs-Sleep avatar Sep 09 '19 04:09 Lindsay-Needs-Sleep

There is a comment in this file as well: // OK, This hack only works on Windows, not on Mac OS or Linux. We will be deleting this eventually!

So perhaps the proper solution is to do what it says.

I don't know if a separate issue should be opened for that or not.

Since I don't really think there is a use case where a user can't remove the parenthesis from their path... It might just be useful to have this issue in existence so that if someone encounters the same problem. (I spent so long futzing with my gradle settings even though it is completely unrelated.)

Lindsay-Needs-Sleep avatar Sep 09 '19 04:09 Lindsay-Needs-Sleep

Since I don't really think there is a use case where a user can't remove the parenthesis from their path...

Back in my school days, it was always recommended to avoid filepaths with special characters because they tend to mess things up in terminal environments.

I just tested having a cordova project in a filepath with parenthesis on Linux and it works fine here. I still think this is an issue worth addressing. Cordova should be sanitizing/escaping special characters in filepaths.

breautek avatar Sep 10 '19 16:09 breautek