AL icon indicating copy to clipboard operation
AL copied to clipboard

forceUpgrade does not work anymore

Open NKarolak opened this issue 2 years ago • 19 comments

1. Describe the bug Since BC 20 or longer, forceUpgrade set in the launch.json does not work anymore: As long as the app version number is unchanged, the upgrade codeunit execution is not triggered.

2. To Reproduce Publish an app in dev scope.

Without changing the app version, add an upgrade codeunit with a breakpoint in the OnUpgradePerCompanyTrigger.

codeunit 50000 "My Upgrade"
{
    Access = Internal;
    Subtype = Upgrade;

    trigger OnUpgradePerCompany()
    begin
        // Set your breakpoint here, or throw message
    end;
}

In the launch.json, set "forceUpgrade": true

Publish all changes with debugging, or publish and wait for a message: The upgrade codeunit is not called.

3. Expected behavior As in earlier versions, the upgrade codeunit should be run.

4. Versions:

  • AL Language: v9.0.615906
  • Visual Studio Code: 1.67.1
  • Business Central: DE Business Central 20.0 (Platform 20.0.37114.40239 + Application 20.0.37253.40281)
  • List of Visual Studio Code extensions that you have installed: unrelated

NKarolak avatar May 11 '22 11:05 NKarolak

Works for me. Have you started an attach configuration with a WebServiceClient client type first and then have you issued the Publish Extension Without Building command?

image

kalberes avatar May 13 '22 13:05 kalberes

No, is this required? Since when, documented? I've worked with forceUpgrade earlier, and I did not change anything.

NKarolak avatar May 13 '22 13:05 NKarolak

Since 2020 release wave 2 (version 6.x). It is in the changelog. I do not know why it was missed from the documentation. Upgrade/Install codeunits are running in internal system sessions, those cannot be debugged. Only if you run the command "Publish Extension Without Building" that is changed. If you could do it probably it was because of a bug that got fixed.
I will ask internally though if anyone knows any state on it

kalberes avatar May 13 '22 14:05 kalberes

Thanks Kalman, I will check that on Monday.

NKarolak avatar May 13 '22 15:05 NKarolak

I can't find any documentation on it either, but I'll try to clarify what I think should happen.

ForceUpgrade should do just that, force the upgrade codeunit to be run even if the version number is the same. This should happen regardless of how you publish from VSCode. The debug window should show that the request query parameters has forceUpgrade set to true:

dev/apps?tenant=default&SchemaUpdateMode=synchronize&ForceUpgrade=true&DependencyPublishingOption=default

But this doesn't mean it is debuggable! The problem with debugging upgrade/install codeunits is that the debugger has to be attached prior to the extension being published. So if you do a regular F5 publish, it won't hit any breakpoints because in that scenario the debugger is only attached after publishing. That's where the "Publish Extension Without Building" command comes in.

So to debug an upgrade/install codeunit, you have to:

  1. Attach first using an "Attach to next" launch configuration targeting a WebServiceClient type
  2. Run the "Publish Extension Without Building" command.

That should do it for debugging. Let me know if this doesn't work.

thloke avatar May 16 '22 00:05 thloke

Thanks Thaddeus,

first of all: I can confirm that the debug window does show ForceUpgrade=true.

Can you please elaborate a bit more on your two steps?

  1. Attach first using an "Attach to next" launch configuration targeting a WebServiceClient type

Did you mean all steps mentioned here? https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-attach-debug-next#to-start-an-attach-session Hence:

  1. Add the attach configuration in launch.json
  2. Publish with (Alt+)Ctrl+F5
  3. Publish with F5 = Publish with Debugging
  1. Run the "Publish Extension Without Building" command.

There is no such command. Did you mean "Debug without publishing"?

NKarolak avatar May 16 '22 07:05 NKarolak

Almost. I did mean "Publish Extension Without Building" but it's a command that only shows up once you have attached to the server. So:

  1. F5 and choose the Attach configuration. This should not trigger the publish.
  2. Once it's attached, run the "Publish Extension Without Building" command.

image

thloke avatar May 16 '22 07:05 thloke

Thanks, got it. Attaching was now finished successfully.

Step 2 - after command "Publish extension without building": The debug console confirms

Success: The package 'xxx.app' has been published to the server.

but is immediately followed by a VS Code error message (not in the debug console):

Command 'AL: Publish extension without building' resulted in an error (Canceled)

But I see no hint why.

NKarolak avatar May 16 '22 08:05 NKarolak

Is this SaaS or local? If it's SaaS, can I get the AAD tenant Id and environment name? If it's local, there might be something in your windows event logs. It's weird, I've never seen that error before.

thloke avatar May 16 '22 08:05 thloke

It's a sandbox in our own Docker swarm.

~~Windows created Security logs of Keyword "Audit Failure" for it, Task Category "Filtering Platform Packet Drop":~~

~~> The Windows Filtering Platform has blocked a packet.~~

~~Application Name: \device\harddiskvolume3\windows\system32\svchost.exe~~

NKarolak avatar May 16 '22 08:05 NKarolak

That doesn't ring a bell for me unfortunately, it's weird because this publish isn't any different request wise from the publish from a regular F5 publish. Let me spin up a SaaS instance to test, and see if we've regressed with this.

thloke avatar May 16 '22 08:05 thloke

Sorry, the Security logs appear independently of VS code and reappear regularly. This must be something else. So I have still no clue what caused the VS Code error message for me.

NKarolak avatar May 16 '22 08:05 NKarolak

@thloke Were you able to reproduce it in a SaaS instance?

NKarolak avatar May 18 '22 08:05 NKarolak

Sorry for the delay in getting back on this, no I was not able to reproduce this on a SaaS or local OnPrem instance. In both cases, I can attach to the upgrade trigger without issues. Can you try against a SaaS sandbox if you have one handy? Then I can inspect logs if it happens there.

thloke avatar May 20 '22 04:05 thloke

I did not test on SaaS sandbox yet, but I made it work in another way:

image

  1. In the primary side bar (left), "RUN AND DEBUG", choose the attach configuration of your app and press run.
  2. Still in RUN AND DEBUG, choose the launch configuration and press run.

This time, the debugger is run and stops at my upgrade codeunit breakpoint instead of throwing an error message :-)

I also tested to set forceUpgrade = false, attach and launch again: Codeunit not run as expected.

Now, what was the difference that made this method work and calling the VS Code commands directly not?

Maybe it would be worth to add my alternative way of attach and debug to article https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-debug-upgrade-install-code?

NKarolak avatar May 20 '22 09:05 NKarolak

I'm actually surprised that this alternative method works. It shouldn't! Or at least it's not designed to work in this way, so I don't think we want to document this. If you can repro this on SaaS, then I can look at the server side logs on our end to see what's happening with the supported way of debugging these codeunits.

thloke avatar Jun 09 '22 07:06 thloke

I work with docker sandboxes only. Maybe it works only there? I could try to reproduce it on SaaS after my holidays in two weeks.

NKarolak avatar Jun 09 '22 07:06 NKarolak

Hi @thloke, I've just tried it on a sandbox on businesscentral.dynamics.com. "My method" using "Run and Debug" also applies there.

Azure AD tenant: 0c0607f9-df1b-44fd-affa-c37c3ab043d6, Environment: SandboxGitHubDE (Sandbox)

NKarolak avatar Jun 20 '22 12:06 NKarolak