Sergio and the sigil

Resharper Test Runner in 64-bit Windows

Posted by Sergio on 2009-07-20

In the spirit of helping the next guy, here's some explanation for a problem that was surprising to me.

We have recently upgraded our product to 64-bits and all developer workstations to Windows 2008 x64. The transition was easy and most things worked with minor or no tweak at all. We're still battling a TFS issue but that will be the subject for another day, once we figure it out.

I noticed that a bunch of my unit test cases were failing when I ran them using Resharper's test runner. I traced the problem down to missing registry values that were needed by the tests. The intriguing part was that I could see the registry settings there (see image below) and the tests were working fine using NUnit's GUI runner.

After a little poking around, I saw this in Process Explorer:

Hmmm. Visual Studio and Resharper run as 32-bit processes and NUnit's (and my application itself) as 64-bit ones. Could there be a difference between Registry access for 32-bit vs. 64-bit processes? Yessir. It turns out that my settings were in the right place but Windows was redirecting the 32-bit Registry accesses to the path shown below, which is obviously empty.

I don't know of any elegant work-around for this issue but for now I'm simply duplicating the values in both places for the sake of testing. I'm not happy with this duplication but it's got me back on track until someone bright chimes in with a better alternative.

Git, SSH, PuTTY, GitHub, Unfuddle, the kitchen sink

Posted by Sergio on 2009-05-06

After reading a good number of the guides for getting Git/ GitHub/ Unfuddle working correctly in Windows, I finally got it sorted out. I had to use a bunch of things I had not used before so I realized it's probably a good idea to share my findings, hoping to help someone else (and maybe myself again) in the future.

Local git is easier

Getting Git to run locally in Windows is not hard. It gets a little trickier when you want to work with remote repositories. Especially if, like me, you have little or no experience using SSH in Windows.

In this post I'll install and configure the tools that enable Git to work with remote repositories (e.g. post changes to a server), which I think is probably what a lot of people will want to do, even if you're just toying with Git or evaluating it.

You will need SSH for serious Gitting

The way Git will authenticate and communicate with hosted repositories like GitHub or Unfuddle is through an SSH tunnel. That means we will need an SSH client to connect to the server and Git will use that connection to securely transfer the repository data up and down.

In Linux or on the Mac, SSH is a trivial thing. It's just part of the default installation. For whatever reason, Windows does not come with an SSH client, so we need to find ourselves a 3rd party client.

PuTTY, a family of tools

Luckily for us, there's a free and very popular SSH client for Windows, called PuTTY. PuTTY is actually a family of utilities that play together to configure and establish SSH (and Telnet) connections.

From the PuTTY downloads page get the following programs and put somewhere in your hard disk. I like to put these types of things in a c:\tools\bin directory, which is included in my %PATH%.

Create your SSH key pair

The most common way to authenticate your SSH connection with the server is using an RSA key pair. The pair contains a public key and a private key. Typically these keys will be stored in two files — make sure that the private key file is in a directory location that only your user account has rights. I'm gonna keep both files in %USERPROFILE%\SSH (type echo %USERPROFILE% in a command window to see where yours is.)

To create a key pair we will use puttygen.exe. Just click Generate and then Save public key and Save private key. I chose to leave the password blank and trust that my directory security is enough to protect my keys. Let's assume I saved my keys at %USERPROFILE%\SSH\private.ppk and %USERPROFILE%\SSH\public.ppk, respectively.

We're not done with PuTTY yet

Before starting an SSH connection with PuTTY, we need to run Pageant.exe. Pageant is a background process that will handle the authentication requests. We will add our private key(s) in Pageant and it will keep them available for new connections, without the need to retype the password (if you entered one) every single time.

Run Pageant.exe, double click its icon in the notification area, and add your private key file. If you specified a password, it will now prompt you for that.

Phew! We are done with the SSH crap. Now on to Git. Finally.

Git via msysgit

There are a few ways of installing the git client in Windows, including downloading and compiling the source. But guess if I want to go down that route. Instead, let's just press the easy button and use a pre-packaged installer.

msysgit is what you'll want to install. Donwload the latest release from the site. The file name of the full installer, as of this writing, will be named like Git-[version]-preview[yyyymmdd].exe.

Run the installer accepting most of the defaults, with the exception of these two screens below. We want to Run Git from the Windows Command Prompt and Use PLink (PuTTY.)

 

After this we should have a working installation of Git. You should be able to use Git from both the Windows command prompt and from the Git Bash shell.

Register your public key with the remote server

Now we need to tell the server that hosts the remote repository that it should expect and accept SSH connections from our machine. The way we do this is similar in GitHub and Unfuddle. We just need to associate our public key with our account on that site.

For example, in my GitHub account settings there's a section called SSH Public Keys, where we can add all public keys that we own. Don't use the public key straight from your PuTTY public key file, it uses a different format. Instead, open Puttygen again and load the private key. The public key, in the right format, will be shown in the big text box at the top of the window. Copy that entire text and paste in a new key in your GitHub settings, as seen below.

Troubleshooting

After all that you should have all it takes to use Git in Windows. Until I got to this point I came across some problems. Here are some of them.

No supported authentication methods available. I got that error while trying to push changes because I either forgot to run Pageant or to add the right key into it. The full error is below.

C:\myproject>git push origin master
FATAL ERROR: Disconnected: No supported authentication methods available
fatal: The remote end hung up unexpectedly

The server's host key is not cached in the registry. Sometimes when connecting to a remote server for the first time, the PuTYY will output this message after your Git command.

The server's host key is not cached in the registry. You
have no guarantee that the server is the computer you
think it is.
The server's rsa2 key fingerprint is:
ssh-rsa 1024 [fingerprint here]
If you trust this host, enter "y" to add the key to
PuTTY's cache and carry on connecting.
If you want to carry on connecting just once, without
adding the key to the cache, enter "n".
If you do not trust this host, press Return to abandon the
connection.
Store key in cache? (y/n)

Blocked .js files keep biting me

Posted by Sergio on 2009-04-03

This is a tip for those working with JavaScript (and possibly other types of files) that is freely available for download. For the nth time in the last few months I spent an unnecessary amount of time trying to find a problem in my code that wasn't really there.

Windows keeps track of the files you save to your hard disk which came from the Internet. Maybe you have seen this problem when you try to open a CHM file that you downloaded and see that it opens to a blank page. The same type of thing happens with JavaScript files that you download. Let's see what happened to me today.

First I downloaded the latest version of QUnit and saved to my application's directory. I did this the usual way, as shown in the image below.

Then I proceeded to use the downloaded testrunner.js file in my HTML page, via a <script src="testrunner.js"></script> tag. When I tried to open my HTML page, I was not expecting to see the particular error shown below.

I started to look for common problems: Did I save the file in the right place? Did I spell its name right? Did I mispell the function name? Did I miss a dependency? No, no, no, and no. All looked good.

That's when I remembered that the file might have been flagged by Windows. I opened its properties dialog and, sure enough, there it was, the infuriating Unblock button.

Ah, that's easy. I just clicked it and refreshed my page. Wah-wah! Problem persists. I tried typing the .js url in the address bar and saw this 401 error message from IIS, not 404.

That told me that there was still something messed up with file access. I checked the file's permissions, comparing to an older .js file in the application. The new one did not grant access to local users, so I added Read access to local users.

Now it works. Depending on how your IIS and/or ASP.NET security is configured, you may not need this last step (e.g. if the process identity already has admin rights, in which case I salute you, brave friend.) The system at hand was a Win2008-x64 installation with the default IIS/ASP.NET settings.

This problem seems to happen to me every other week and I still did not develop the reflex to take care of it right when I download the file. On the other hand, I'm getting better at remembering about it when I see stuff like "function not defined" in an error message.

Resharper an OutOfMemory Exceptions

Posted by Sergio on 2009-02-03

My current project has an interesting story of being converted across platforms and programming languages. I hope I can talk about this conversion process some day (it was not manual).

One of the side effects of mass conversions like this is that the final result tends to carry over some weird patterns. These patterns used to make total sense in the previous programming language, but look very unnatural in the new one.

We ended up with some very large classes inside a single namespace that, while work exactly as in the previous language, cause an unusual amount of stress inside Visual Studio, especially with Resharper (version 4.1) installed. Resharper would halt with OutOfMemory exceptions all the time, until I disabled Code Analysis, arguably one of the most important features of that product.

Finally today a coworker pointed me to a fix for that. I'm still getting the exceptions if I turn on solution-wide error analysis, but I think I can live without that.

I haven't had a chance to try the nightly builds of Resharper 4.5. I have been told that those issues may go away.

UPDATE: I installed version 4.5 (build 1169.15) but it didn't help me. The only thing that changed was the layout of the error screen :) But I know my project is a tad extreme and I deserve to be punished for it.

jQuery Character Table

Posted by Sergio on 2008-12-29

The other day I was looking for the HTML code for the ⌘ (in case you can't see it, that's the command key symbol), found in mac keyboards. It was not the first time I was looking for the HTML code for one of those funky characters. I remember having a hard time trying to represent some Math symbols, like sums and integral equations from my college Calculus days.

I thought this would be a nice opportunity to create a small jQuery sample that renders a range (or ranges) of HTML characters along with their codes.

I started with a simple HTML page, leaving all the JavaScript in an external file.

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>HTML Characters</title>
    <meta http-equiv="Content-Type" 
      content="text/html; charset=utf-8">
    <script type="text/javascript" 
      src="jquery-1.2.6.js"></script>
    <script type="text/javascript" 
      src="char-table.js"></script>
  </head>
  <body>
    From (hex): <input id="range_from" type="text"  /> <br/>
    To (hex): <input id="range_to" type="text"  /> <br/>
    <input type="button" value="Load" id="btnLoad" />
    <input type="button" value="Load Example" id="btnLoadEx" />
    <input type="button" value="Load Keyboard Symbols" 
         onclick="CHARTABLE.loadKeyboardSymbols();" />
    <table id="char_table" border="1" />
  </body>
</html>

Here's the JavaScript in the file char-table.js.

$(function(){
  //here we put everything that will run when the page is ready

  $('#btnLoad').click(function(){
    // notice how we get the value of the input fields using val()
    // notice how to parse hex numbers in JS
    var from = parseInt($('#range_from').val(), 16);
    var to = parseInt($('#range_to').val(), 16);
    CHARTABLE.loadRange(from, to);
  });

  $('#btnLoadEx').click(function(){
  //setting the value of each input field
    $('#range_from').val('2190');
    $('#range_to').val('2195');
  //fire the click event of the Load button
    $('#btnLoad').click();
  });

});

// the CHARTABLE object acts as a namespace
var CHARTABLE = {
   loadRanges: function() {
    this.clearTable();
    for(var i=0; i<arguments.length; i++) {
      this.appendRange(
        parseInt(arguments[i][0], 16), 
        parseInt(arguments[i][1], 16)
        );
    }
  },

  loadRange: function(from, to) {
    this.clearTable();
    this.appendRange(from, to);
  },

  clearTable: function(){
    //the html() function is how we replace the innerHTML with jQuery
    $('#char_table').        
      html('<tr><th>Character</th><th>Code</th></tr>');
  },

  appendRange: function(from, to){
    //we can create stand alone DOM elements with $()
    var tbody = $('<tbody/>');
    for(var i=from; i<=to; i++) {
      //notice how we can convert a number to hex
      $('<tr>').
        append('<td>&#x' + i.toString(16) + ';</td>').
        append('<td>&amp;#x' + i.toString(16) + ';</td>').
        appendTo(tbody);
    }
    //adding the tbody to the table at the end
    //  renders faster than adding each row to
    //  the table as we go
    $('#char_table').append(tbody);
  },

  loadKeyboardSymbols: function(){
    this.loadRanges(
      ['21e7', '21e7'],
      ['21a9', '21a9'],
      ['2303', '2305'],
      ['2318', '2318'],
      ['2324', '2327'],
      ['232b', '232b'],
      ['2190', '2198'],
      ['21de', '21df'],
      ['21e4', '21e9']
    );
  }
};

Now enter the range from 2100 to 2400 and stare at a whole bunch of characters that can become useful in some situations.

Note: The characters don't render equally in IE and Firefox, but a large number of them render just fine in both browsers.