Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Application.ExecutablePath returns dll instead of exe (#2801) #2838

Closed

Conversation

RussKie
Copy link
Member

@RussKie RussKie commented Feb 10, 2020

Resolves #1143

For discussions and tests refer to #2801
(cherry picked from commit 2af3af9)

Proposed changes

In .NET artifacts are DLLs even for executable projects. With some automagic they get bundled into executables.
However Assembly.GetEntryAssembly() always returns the dll instead of the exe.

Following the guidance from the Runtime team retrieve the path to the executable via GetModuleFileNameW call.

Customer Impact

  • Customers can now retrieve a path to the applications executable instead of the application's dll.

Regression?

  • Yes

Risk

  • Small-medium due to possibly unaccounted use cases

Test methodology

Microsoft Reviewers: Open in CodeFlow

@RussKie RussKie added the servicing-consider .NET Shiproom label indicating a PR seeks to enter into a branch under Tell-Mode criteria label Feb 10, 2020
@RussKie RussKie added this to the 3.1.x milestone Feb 10, 2020
@RussKie RussKie requested a review from a team as a code owner February 10, 2020 05:26
@ghost ghost assigned RussKie Feb 10, 2020
@codecov
Copy link

codecov bot commented Feb 10, 2020

Codecov Report

Merging #2838 into release/3.1 will increase coverage by 0.00998%.
The diff coverage is 0%.

@@                 Coverage Diff                  @@
##           release/3.1      #2838         +/-   ##
====================================================
+ Coverage     24.85382%   24.8638%   +0.00997%     
====================================================
  Files              844        844                 
  Lines           260290     260274         -16     
  Branches         36896      36894          -2     
====================================================
+ Hits             64692      64714         +22     
+ Misses          190874     190837         -37     
+ Partials          4724       4723          -1
Flag Coverage Δ
#Debug 24.8638% <0%> (+0.00998%) ⬆️
#production 24.8638% <0%> (+0.00998%) ⬆️
#test ?

…2801)

In .NET artifacts are DLLs even for executable projects. With some automagic
they get bundled into executables.
However `Assembly.GetEntryAssembly()` always returns the dll instead of the exe.

Following the guidance from the Runtime team retrieve the path to the
executable via `GetModuleFileNameW` call.

Resolves dotnet#1143

(cherry picked from commit 2af3af9)
@RussKie RussKie force-pushed the fix_Application.ExecutablePath branch from a17e689 to abd221b Compare February 11, 2020 00:49
}
}
StringBuilder sb = UnsafeNativeMethods.GetModuleFileNameLongPath(NativeMethods.NullHandleRef);
executablePath = Path.GetFullPath(sb.ToString());
Copy link
Member

@davkean davkean Feb 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this does return a relative path at any point, GetFullPath is going to resolve it by whatever the current directory is, which may or may not be the application directory:

C:\>cd NotApplicationDir
C:\NotApplicationDir> C:\MyApp\App.exe

If GetModuleFileNameLongPath actually returns a relative path at any point, this will return C:\NotApplicationDir\App.exe.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my reading, it probably doesn't, and you are doing this just to normalize the path, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tested a number of different configurations and posted results here, here and here.

Please let me know if I missed anything.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my reading, it probably doesn't, and you are doing this just to normalize the path, right?

I believe so too. That's how the original code had it. It may be redundant but I'm not going to change it on a service branch. :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doc says

Retrieves the fully qualified path

which rules out relative paths (actually I commented on this on the other PR)

@merriemcgaw merriemcgaw removed the servicing-consider .NET Shiproom label indicating a PR seeks to enter into a branch under Tell-Mode criteria label Feb 11, 2020
Copy link
Member

@Shyam-Gupta Shyam-Gupta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@RussKie RussKie added 📫 waiting-approval 🚫 * NO-MERGE * The PR is not ready for merge yet (see discussion for detailed reasons) and removed 📫 waiting-approval 🚫 * NO-MERGE * The PR is not ready for merge yet (see discussion for detailed reasons) labels Feb 12, 2020
@RussKie RussKie closed this Feb 13, 2020
@RussKie RussKie removed this from the 3.1.x milestone Feb 13, 2020
@merriemcgaw
Copy link
Member

This is good for 5.0, but until we find out that it's blocking servicing, I think that this won't meet a servicing bar for 3.1.

@RussKie RussKie deleted the fix_Application.ExecutablePath branch July 23, 2020 06:06
@ghost ghost locked as resolved and limited conversation to collaborators Feb 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants